I want to add selectors to an NSMutableArray. But since they're opaque types and no objects, that wouldn't work, right? Is there an wrapper object I can use? Or do I have to create my own?
+7
A:
You can wrap it in an NSValue
instance as follows:
SEL mySelector = @selector(performSomething:);
NSValue *value = [NSValue value:&mySelector withObjCType:@encode(SEL)];
and then add value to your NSMutableArray
instance.
pgb
2009-05-29 22:46:00
+3
A:
You can store the NSString name of the selector in the array and use
SEL mySelector = NSSelectorFromString([selectorArray objectAtIndex:0]);
to generate the selector from the stored string.
Additionally, you can package up the selector as an NSInvocation using something like
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:mySelector]];
[invocation setTarget:self];
[invocation setSelector:mySelector];
[invocation setArgument:&arg atIndex:2];
[invocation retainArguments];
This NSInvocation object can then be stored in the array and invoked later.
Brad Larson
2009-05-30 00:57:24
Interesting - I've never used NSInvocation - thanks!
Ben Gotow
2009-06-01 23:15:52