作者b26168 (b26168)
看板Python
标题[问题] embedding in c, 用 ctypes delegate
时间Mon Jan 15 16:19:56 2018
参考了这个问题的第二个做法
成功了将 C 端的 object delegate 过来给 python
https://stackoverflow.com/questions/21988970/how-do-i-pass-an-objective-c-instance-into-a-pyobjc-python-function-when-using-p
但如果今天 delegate.aMethod 变成要传入参数的情况
一直不知道该怎麽做
尝试了 delegate.aMethod.restype 跟 .argtypes 的宣告
似乎就直接 crash 掉了 (python 新手, 不太知道如何去 debug 这个错误)
不知道有没有人知道这个冷门的问题该如何解??
感谢!
附上使用范例
def myCallable(delegateID):
_objc = ctypes.PyDLL(objc._objc.__file__)
_objc.PyObjCObject_New.restype = ctypes.py_object
_objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int,
ctypes.c_int]
delegate = _objc.PyObjCObject_New(delegateID, 0, 1)
delegate.aMethod.restype = ctypes.bool
delegate.aMethod.argtypes = [ctypes.c_int]
delegate.aMethod(ctypes.c_int(123))
Objective - C 的宣告
-(bool)aMethod:(int)a;
以上
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.128.199.139
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1516004399.A.5D1.html
1F:→ uranusjr: 啊你也不给个会 crash 的范例现在是要通灵吗 01/15 16:28
crash 在 Objective-C 端, 所以就省略掉了
EXC_BAD_ACCESS
有在 python 端加上 try catch
AttributeError
※ 编辑: b26168 (220.128.199.139), 01/15/2018 16:59:48
2F:→ uranusjr: 试试把 Python 端的 aMethod 改成 aMethod_ 01/15 20:53
没有作用
原本没有带参数的 aMethod 是可以跑的
现在在思考 似乎不是 ctypes 的问题
而是 PyObjcObject 这个东西所取得的物件
他的 function 要怎麽带参数给他
※ 编辑: b26168 (220.128.199.139), 01/16/2018 09:48:26
3F:→ uranusjr: 其实如果你不介意直接换个 library 这个用 Rubicon 会方 01/16 11:50
4F:→ uranusjr: 便很多; 我猜这个是要设 Objective-C 的 signature 让 01/16 11:50
5F:→ uranusjr: signal 知道你送进去的是 int, 但是其实也有点麻烦 01/16 11:51