使用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 call
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];
p>
推荐答案
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, ...)
如果选择器返回一个float,你可以需要使用 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屋!