github.com/jwijenbergh/purego@v0.0.0-20240126093400-70ff3a61df13/examples/objc/main_darwin.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors 3 4 package main 5 6 import ( 7 "fmt" 8 "reflect" 9 10 "github.com/jwijenbergh/purego/objc" 11 ) 12 13 var ( 14 sel_new = objc.RegisterName("new") 15 sel_init = objc.RegisterName("init") 16 sel_setBar = objc.RegisterName("setBar:") 17 sel_bar = objc.RegisterName("bar") 18 ) 19 20 func BarInit(id objc.ID, cmd objc.SEL) objc.ID { 21 return id.SendSuper(cmd) 22 } 23 24 func main() { 25 // This struct is equivalent to the following Objective-C definition. 26 // 27 // @interface BarObject : NSObject <NSDelegateWindow> 28 // @property (readwrite) bar int 29 // @end 30 class, err := objc.RegisterClass( 31 "BarObject", 32 objc.GetClass("NSObject"), 33 []*objc.Protocol{ 34 objc.GetProtocol("NSDelegateWindow"), 35 }, 36 []objc.FieldDef{ 37 { 38 Name: "bar", 39 Type: reflect.TypeOf(int(0)), 40 Attribute: objc.ReadWrite, 41 }, 42 }, 43 []objc.MethodDef{ 44 { 45 Cmd: sel_init, 46 Fn: BarInit, 47 }, 48 }, 49 ) 50 if err != nil { 51 panic(err) 52 } 53 54 object := objc.ID(class).Send(sel_new) 55 object.Send(sel_setBar, 123) 56 bar := int(object.Send(sel_bar)) 57 fmt.Println(bar) 58 // Output: 123 59 }