NSIncation 类上的 setSelector 方法的用途是什么?
我不明白为什么当该信息已经通过 invokingWithMethodSignature
传递时,我们必须在 NSInitation
对象上调用 setSelector
方法。
要创建 NSInitation
对象,我们执行以下操作:
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 instanceMethodSignatureForSelector: someSelector];
,然后再次传递给 [inspiration setSelector:someSelector] ;
。
我有什么遗漏的吗?
I don't understand why we have to call the setSelector
method on NSInvocation
objects when that information is already passed via the invocationWithMethodSignature
.
To create an NSInvocation
object we do the following:
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];
Notice that we passed the selector to [SomeObject instanceMethodSignatureForSelector: someSelector];
and again to [invocation setSelector:someSelector];
.
Is there something I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
签名不是选择器。选择器是消息的名称。签名定义了参数和返回值。您可以有许多具有相同签名的选择器,反之亦然。如果您查看
NSMethodSignature
,您会注意到没有-selector
方法;签名不携带特定的选择器。考虑以下情况:
它们具有相同的选择器
@selector(setLocation:)
,但签名不同。它们具有相同的签名,但选择器不同。
ObjC 编程语言中的选择器可能是理解这一点的有用参考。
A signature is not a selector. A selector is the name of a message. The signature defines the parameters and the return value. You can have many selectors with the same signature, and vice versa. If you look at
NSMethodSignature
, you will note that there is no-selector
method; signatures do not carry around a particular selector.Consider the following
They have the same selector
@selector(setLocation:)
, but different signatures.These have the same signature, but different selectors.
Selectors from the ObjC Programming Language may be a useful reference for understanding this.
方法签名仅定义返回类型以及参数的数量和类型。它不包含有关选择器名称的任何内容。例如,尽管具有不同的选择器,但所有这些方法都具有相同的签名:
A method signature only defines the return type, and the number and type of arguments. It doesn't include anything about the selector name. For example, all of these methods have the same signature, despite having different selectors:
这是一种间接答案,但您可以执行以下操作这一事实帮助我更好地理解方法签名和选择器之间的分离。
此代码位于视图控制器中
,因为 UIViewController 的 viewDidAppear:
和UIPopoverController的dismissPopoverAnimated:两者都采用BOOL参数并返回void,您可以使用一个选择器创建方法签名,但将调用发送到另一个选择器。
This is kind of a side-answer, but the fact that you can do the following helped me understand better the separation between method signatures and selectors.
This code is within a View Controller
Since UIViewController's viewDidAppear:
and UIPopoverController's dismissPopoverAnimated: both take a BOOL argument and return void, you can create the method signature using one selector, but send the invocation to another.