Contents

getArgument:atIndex:

Returns by indirection the receiver’s argument at a specified index.

Declaration

- (void) getArgument:(void *) argumentLocation atIndex:(NSInteger) idx;

Parameters

  • argumentLocation:

    An untyped buffer to hold the returned argument. See the discussion below relating to argument values that are objects.

  • idx:

    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

This method copies the argument stored at index into the storage pointed to by buffer. The size of buffer must 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.

In the following example, myInvocation represents a call to a two-argument method called createWithString:count: in a class called MyClass, which takes an NSMutableString and an int. The example performs the invocation with invoke, then retrieves the first argument with getArgument:atIndex: and copies it to a strongly-held property called myObject.

// Create invocation and set selector.
MyClass* myTarget = [[MyClass alloc] init];
NSMethodSignature* mySignature = [MyClass
                                  instanceMethodSignatureForSelector: @selector(createWithString:count:)];
NSInvocation* myInvocation = [NSInvocation invocationWithMethodSignature: mySignature];
[myInvocation setSelector: @selector(createWithString:count:)];

// Set arguments and invoke.
NSMutableString* argument2 = [NSMutableString stringWithString: @"A string"];
[myInvocation setArgument:&argument2 atIndex: 2];
int argument3 = 5;
[myInvocation setArgument:&argument3 atIndex: 3];

[myInvocation invokeWithTarget: myTarget];

// Retrieve first argument and assign to property.
__unsafe_unretained id tempObject = nil;
[myInvocation getArgument: &tempObject atIndex: 2];
self.myObject = tempObject;
NSLog(@"Argument 0 was: %@", self.myObject); // prints "A string"

This method raises invalidArgumentException if index is greater than the actual number of arguments for the selector.

See Also

Related Documentation

Configuring an Invocation Object