har*_*ell 10 methods language-design dynamic objective-c nsinvocation
我不明白为什么我们必须setSelector在NSInvocation对象上调用该方法,因为该信息已经通过invocationWithMethodSignature.
要创建NSInvocation对象,我们执行以下操作:
SEL someSelector;
NSMethodSignature *signature;
NSInvocation *invocation;
someSelector = @selector(sayHelloWithString:);
//Here we use the selector to create the signature
signature = [SomeObject instanceMethodSignatureForSelector:someSelector];
invocation = [NSInvocation invocationWithMethodSignature:signature];
//Here, we again set the same selector
[invocation setSelector:someSelector];
[invocation setTarget:someObjectInstance];
[invocation setArgument:@"Loving C" atIndex:2];
Run Code Online (Sandbox Code Playgroud)
请注意,我们将选择器传递给[SomeObject instanceMethodSignatureForSelector: someSelector];并再次传递给[invocation setSelector:someSelector];.
有什么我想念的吗?
签名不是选择器.选择器是消息的名称.签名定义参数和返回值.您可以拥有许多具有相同签名的选择器,反之亦然.如果你看NSMethodSignature,你会注意到没有-selector方法; 签名不带有特定的选择器.
考虑以下
- (void)setLocation:(CGFloat)aLocation;
- (void)setLocation:(MyLocation*)aLocation;
Run Code Online (Sandbox Code Playgroud)
它们具有相同的选择器@selector(setLocation:),但具有不同的签名.
- (void)setX:(CGFloat)x;
- (void)setY:(CGFloat)y;
Run Code Online (Sandbox Code Playgroud)
它们具有相同的签名,但选择器不同.
ObjC编程语言中的选择器可能是理解这一点的有用参考.
| 归档时间: |
|
| 查看次数: |
715 次 |
| 最近记录: |