微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – NSInvocation类上的setSelector方法的目的是什么?

我不明白为什么当这些信息已经通过invocationWithMethodSignature传递时,我们必须在NSInvocation对象上调用setSelector方法.

要创建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];

请注意,我们将选择器传递给[SomeObject instanceMethodForSelector:someSelector];并再次调用setSelector:someSelector] ;.

有没有我失踪的东西?

解决方法

签名不是选择器.选择器是消息的名称.签名定义参数和返回值.您可以拥有许多具有相同签名的选择器,反之亦然.如果你看NSMethodSignature,你会注意到没有-selector方法;签名不带有特定的选择器.

请考虑以下几点

- (void)setLocation:(CGFloat)aLocation;
- (void)setLocation:(MyLocation*)aLocation;

他们有相同的选择器@selector(setLocation :),但不同的签名.

- (void)setX:(CGFloat)x;
- (void)setY:(CGFloat)y;

这些具有相同的签名,但不同的选择器.

来自ObjC编程语言的Selectors可能是理解这一点的有用参考.

原文地址:https://www.jb51.cc/c/115379.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐