按名称调用 Objective-C 方法
当我拥有的只是字符串形式的签名时,如何在运行时调用 Objective-C 类上的方法:
NSString* typeName = @"Widgets";
NSString* methodName = [NSString stringWithFormat:@"add%@Object:", typeName];
请注意,方法名称可以在运行时更改,但参数数量保持固定 - 在本例中为一个。
How can I invoke a method at runtime on an Objective-C class when all I have is it's signature in string form:
NSString* typeName = @"Widgets";
NSString* methodName = [NSString stringWithFormat:@"add%@Object:", typeName];
Note that the method name can change at runtime but the number of arguments stays fixed - one in this instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用类似以下内容:
如果需要传递参数,还有
performSelector:withObject:
和performSelector:withObject:withObject:
方法。You can use something like the following:
There are also
performSelector:withObject:
, andperformSelector:withObject:withObject:
methods if you need to pass parameters.为了在 Objective C 上进行反射执行方法调用,只需使用这个快速方法。 objC 使我们能够在运行时检查对象是否支持特定接口,如果存在,则此调用会动态发生。
In order to perform methods invocation in reflection on objetive c just use this quick recipe. objC enabling us checking if an object supports a particular interface at runtime, This invocation happens dynamically if exists.