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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With