使用反射/内省调用具有未知数量参数的选择器
最近我用java(适用于android)编写了一个应用程序,它使用反射来调用某些对象的方法。参数数量和类型未知,这意味着我有一个统一的机制,可以接收对象名称、方法名称和参数数组(使用 JSON),并使用参数数组 (Object[]填充所需类型的参数)。
现在我需要为 iOS 实现相同的功能,当我知道选择器期望的参数数量时,我能够调用选择器,如下所示:
SEL selector = NSSelectorFromString(@"FooWithOneArg");
[view performSelectorInBackground:selector withObject:someArg];
我知道我可以通过使用获取选择器接收的参数数量
int numberOfArguments = method_getNumberOfArguments(selector);
但是有没有办法进行像这样的通用调用:
[someObject performSelector:selector withObject:arrayOfObjects]
这与 Java 的
someMethod.invoke(someObject, argumentsArray[]);
?
我想根据选择器获得的参数数量避免切换情况。
抱歉这么长时间的挖掘,我只是想让我的问题尽可能清楚。
Lately I wrote an application in java (for android) which used reflection to invoke methods of some objects. The argument number and type was unknown, meaning, I had a unified mechanism that received an object name, method name and array of parameters (using JSON) and invoked the specified method on the specified object with an array of the arguments (Object[] filled with arguments of the required types).
Now I need to implement the same for iOS, I was able to invoke a selector when I knew the number of parameters the selector expected for like this:
SEL selector = NSSelectorFromString(@"FooWithOneArg");
[view performSelectorInBackground:selector withObject:someArg];
I know I can get the number of arguments the selector receives by using
int numberOfArguments = method_getNumberOfArguments(selector);
But is there a way to make a generic call like this:
[someObject performSelector:selector withObject:arrayOfObjects]
which is pretty much equivalent to Java's
someMethod.invoke(someObject, argumentsArray[]);
?
I want to avoid a switch case according to the amount of arguments the selector gets.
Sorry for the long dig, I just want to make my question as clear as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个小函数应该可以解决问题,它并不完美,但它为您提供了一个起点:
This small function should do the trick, its not perfect, but it gives you a starting point:
借助这里的出色帮助,包括 user102008 的简单但完美的答案,我整理了以下示例。请注意,我真正想做的是允许某人向我发送一个目标选择器,该选择器要么接受参数,要么不接受参数。如果需要一个参数,我假设他们希望调用对象的“self”作为引用返回:
希望这可以帮助人们进行挖掘。
With the awesome help here including the simple but perfect answer from user102008 I pulled together the following example. Note what I was really trying to do was allow someone to send me a target selector that either did or did not take an argument. If it takes an argument I assume they want the calling object's "self" returned as a reference:
Hope this helps someone digging around.
我修改了 @JustSid 答案并添加了更多验证、nil 参数支持,将其更改为 Obj-C NSObject 类别方法,并添加
-performSelectorIfAvailable:
辅助方法以方便使用。请欣赏! :)I modified @JustSid answer and added more validation, nil argument support, changed it to an Obj-C NSObject category method, and add
-performSelectorIfAvailable:
helper methods for easier use. Please enjoy! :)为什么不定义每个方法以采用一个参数:对象数组? 方法
想必您想要的是使用从数组中设置的参数来调用它的 。好吧,改为:
那么你的整个调度机制就变成:
Why not define each of your methods to take one argument: the array of objects? Presumably what you want is, with with the method
to invoke it with the parameters set from the array. Well, instead have:
then your whole dispatch mechanism just becomes: