Forum Archive

How can I use return in my objC class

watosar
from objc_util import *

def foo_bar(_self,_cmd):
    baz = c_int()
    return baz

foo=create_objc_class('foo',methods=[foo_bar])
foo = foo.alloc()
foo.bar.restype = c_int

print('out',foo.bar())

This returned

out None

I couldn’t found what I have to do.
Please teach me.

JonB

first, you need to call .alloc().init(). well, in this case you don't, but if you send this object to other objc code it will crash if you dont,

second, you need to set encoding, and optionally restype, AND argtypes. (restype only works if argtypes is also specified). This must be done before you call create_objc_class. Most import is the encoding.

Also, you just return normal python types, and the callback mechanism takes care of converting.

Here is you code, updated, along with a description of the type encodings. see objc_util source for more details
https://gist.github.com/d1b10d812306a749335e052eb5e1af9e

watosar

Thank you!!!!