How can I dynamically create a selector at runtime with Objective-C?

cocoa, dynamic, objective-c

Solution

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.

Problem

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? What I can do: ``` 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]; ``` 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.

Original source