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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With