如何在运行时使用Objective-C动态创建选择器? [英] How can I dynamically create a selector at runtime with Objective-C?
问题描述
我知道如何使用 @selector(MyMethodName:)
在编译时创建 SEL
要做的是从 NSString
动态创建一个选择器。这是可能吗?
I know how to create a SEL
at compile time using @selector(MyMethodName:)
but what I want to do is create a selector dynamically from an NSString
. Is this even possible?
我可以做什么:
SEL selector = @selector(doWork:);
[myobj respondsToSelector:selector];
我想做什么:(伪代码,这显然不起作用)
What I want to do: (pseudo code, this obviously doesn't work)
SEL selector = selectorFromString(@"doWork");
[myobj respondsToSelector:selector];
我一直在搜索Apple API文档,但没有找到一种方法依赖于编译时 @selector(myTarget:)
语法。
I've been searching the Apple API docs, but haven't found a way that doesn't rely on the compile-time @selector(myTarget:)
syntax.
推荐答案
p>我不是一个Objective-C程序员,只是一个同情者,但也许 NSSelectorFromString 是你需要的。有关详情,请参阅运行时参考中的内容,您可以使用它将字符串转换为选择器。
I'm not an Objective-C programmer, merely a sympathizer, but maybe NSSelectorFromString is what you need. It's mentioned explicity in the Runtime Reference that you can use it to convert a string to a selector.
这篇关于如何在运行时使用Objective-C动态创建选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!