是否有SEL的包装器对象?

Tha*_*nks 4 iphone cocoa-touch selector uikit

我想将选择器添加到NSMutableArray.但由于它们是不透明的类型而没有物体,这不起作用,对吧?我可以使用包装器对象吗?或者我必须创建自己的?

pgb*_*pgb 9

您可以将其包装在NSValue实例中,如下所示:

SEL mySelector = @selector(performSomething:);
NSValue *value = [NSValue value:&mySelector withObjCType:@encode(SEL)];
Run Code Online (Sandbox Code Playgroud)

然后为您的NSMutableArray实例添加值.


Bra*_*son 5

您可以将选择器的NSString名称存储在数组中并使用

SEL mySelector = NSSelectorFromString([selectorArray objectAtIndex:0]);
Run Code Online (Sandbox Code Playgroud)

从存储的字符串生成选择器.

此外,您可以使用类似的方式将选择器打包为NSInvocation

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:mySelector]];

[invocation setTarget:self];
[invocation setSelector:mySelector];
[invocation setArgument:&arg atIndex:2];
[invocation retainArguments];
Run Code Online (Sandbox Code Playgroud)

然后,可以将此NSInvocation对象存储在数组中,稍后再调用.