使用 objc_msgSend 调用带有命名参数的 Objective C 函数 [英] using objc_msgSend to call a Objective C function with named arguments
问题描述
我想为使用 objc 运行时的 Objective-C 项目添加脚本支持.现在我面临的问题是,我不知道应该如何调用带有多个命名参数的 Objective-C 方法.
I want to add scripting support for an Objective-C project using the objc runtime. Now I face the problem, that I don't have a clue, how I should call an Objective-C method which takes several named arguments.
例如下面的objective-c调用
So for example the following objective-c call
[object foo:bar];
可以从 C 调用:
objc_msgSend(object, sel_getUid("foo:"), bar);
但是我将如何为方法调用做类似的事情:
But how would I do something similar for the method call:
[object foo:var bar:var2 err:errVar];
??
最佳马库斯
推荐答案
objc_msgSend(object, sel_getUid("foo:bar:err:"), var, var2, errVar);
如果其中一个变量是 float
,则需要使用 @Ken 的方法,或欺骗重新解释:
If one of the variables is a float
, you need to use @Ken's method, or cheat by a reinterpret-cast:
objc_msgSend(..., *(int*)&var, ...)
另外,如果选择器返回一个浮点数,你可能需要使用objc_msgSend_fpret
,如果它返回一个结构你必须使用objc_msgSend_stret
.如果这是对超类的调用,则需要使用 objc_msgSendSuper2
.
Also, if the selector returns a float, you may need to use objc_msgSend_fpret
, and if it returns a struct you must use objc_msgSend_stret
. If that is a call to superclass you need to use objc_msgSendSuper2
.
这篇关于使用 objc_msgSend 调用带有命名参数的 Objective C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!