Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an wrapper object for SEL?

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?

like image 943
Thanks Avatar asked Dec 05 '22 06:12

Thanks


2 Answers

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.

like image 67
pgb Avatar answered Dec 22 '22 23:12

pgb


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.

like image 27
Brad Larson Avatar answered Dec 22 '22 22:12

Brad Larson