NSIncation 类上的 setSelector 方法的用途是什么?

发布于 2024-10-31 05:09:07 字数 876 浏览 0 评论 0原文

我不明白为什么当该信息已经通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗梦空间 2024-11-07 05:09:07

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

考虑以下情况:

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

它们具有相同的选择器 @selector(setLocation:),但签名不同。

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

它们具有相同的签名,但选择器不同。

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

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

They have the same selector @selector(setLocation:), but different signatures.

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

These have the same signature, but different selectors.

Selectors from the ObjC Programming Language may be a useful reference for understanding this.

成熟稳重的好男人 2024-11-07 05:09:07

方法签名仅定义返回类型以及参数的数量和类型。它不包含有关选择器名称的任何内容。例如,尽管具有不同的选择器,但所有这些方法都具有相同的签名:

-(void) foo:(NSString*)fooString;
-(void) bar:(NSString*)barString;
-(void) baz:(NSString*)bazString;

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:

-(void) foo:(NSString*)fooString;
-(void) bar:(NSString*)barString;
-(void) baz:(NSString*)bazString;
甜扑 2024-11-07 05:09:07

这是一种间接答案,但您可以执行以下操作这一事实帮助我更好地理解方法签名和选择器之间的分离。

此代码位于视图控制器中

NSMethodSignature *sig = nil;
sig = [[self class] instanceMethodSignatureForSelector:@selector(viewDidAppear:)];
NSInvocation *myInvocation = nil;
myInvocation = [NSInvocation invocationWithMethodSignature:sig];

[myInvocation setTarget:_somePopoverController];
[myInvocation setSelector:@selector(dismissPopoverAnimated:)];
BOOL animate = YES;
[myInvocation setArgument:&animate atIndex:2];
[myInvocation invoke];

,因为 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

NSMethodSignature *sig = nil;
sig = [[self class] instanceMethodSignatureForSelector:@selector(viewDidAppear:)];
NSInvocation *myInvocation = nil;
myInvocation = [NSInvocation invocationWithMethodSignature:sig];

[myInvocation setTarget:_somePopoverController];
[myInvocation setSelector:@selector(dismissPopoverAnimated:)];
BOOL animate = YES;
[myInvocation setArgument:&animate atIndex:2];
[myInvocation invoke];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文