Koh*_* Yu 0 objective-c selector
我知道如何使用:
[self method:object];
Run Code Online (Sandbox Code Playgroud)
但是有可能获得这个的SEL对象吗?
SEL method = @selector(method:object);
Run Code Online (Sandbox Code Playgroud)
不行.
谢谢 :)
A SEL只是选择器 - 发送的消息的名称.要捕获该消息的特定实例,其参数及其作为对象的返回值,您需要使用NSMethodSignature和NSInvocation.一个例子,基于您的假设-method:object:
NSMethodSignature *sig = [SomeClass instanceMethodSignatureForSelector:@selector(method:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
// Assume that someObject is an instance of SomeClass
[inv setTarget:someObject];
// Assume an "id object" declared elsewhere.
// Also note that self & _cmd are at indices 0 & 1, respectively
[inv setArgument:&object atIndex:2]
// Some time later...
[inv invoke];
Run Code Online (Sandbox Code Playgroud)
请注意,因为NSInvocation是一个对象,所以不必立即调用它.它可以存储起来供以后使用,通常是 - 如果想立即发送消息,有更简单的方法来发送消息.例如,Cocoa的标准撤销/重做机制基于存储和调用NSInvocations.
| 归档时间: |
|
| 查看次数: |
402 次 |
| 最近记录: |