Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of the setSelector method on the NSInvocation class?

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?

like image 657
haroldcampbell Avatar asked Apr 07 '11 18:04

haroldcampbell


1 Answers

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.

like image 58
Rob Napier Avatar answered Sep 22 '22 05:09

Rob Napier