Saturday, May 11, 2013

NSInvocation getArgument example ios


getArgument :atIndex:

Returns by indirection the receiver's argument at a specified index.
- (void)getArgument:(void *)buffer atIndex:(NSInteger)index
Parameters
buffer
An untyped buffer to hold the returned argument. See the discussion below relating to argument values that are objects.
index
An integer specifying the index of the argument to get.
Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively; these values can be retrieved directly with the target and selector methods. Use indices 2 and greater for the arguments normally passed in a message.
Discussion( NSInvocation getArgument example )
This method copies the argument stored at index into the storage pointed to by buffer. The size of buffermust be large enough to accommodate the argument value.
When the argument value is an object, pass a pointer to the variable (or memory) into which the object should be placed:
NSArray *anArray;
[invocation getArgument:&anArray atIndex:3];
This method raises NSInvalidArgumentException if index is greater than the actual number of arguments for the selector.
( NSInvocation getArgument example )
NSString * firstArgument = nil;
NSString * secondArgument = nil;
[theInvocation getArgument:&firstArgument atIndex:2];
[theInvocation getArgument:&secondArgument atIndex:3];
NSLog(@"Arguments: %@ and %@", firstArgument, secondArgument);
( NSInvocation getArgument example )
NSDecimalNumber* val;
[anInvocation getArgument:&val atIndex:2];
anInvocation = nil;
NSLog(@"%@", val); // kaboom!


__unsafe_unretained NSDecimalNumber* tempVal;
[anInvocation getArgument:&tempVal atIndex:2];
NSDecimalNumber* val = tempVal;
anInvocation = nil;
NSLog(@"%@", val); // fine