加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:50:26 所属栏目:百科 来源:网络整理
导读:我不明白为什么当这些信息已经通过invocationWithMethodSignature传递时,我们必须在NSInvocation对象上调用setSelector方法. 要创建NSInvocation对象,我们执行以下操作: SEL someSelector;NSMethodSignature *signature;NSInvocation *invocation;someSelec
我不明白为什么当这些信息已经通过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可能是理解这一点的有用参考.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读