---
title: "getArgument:atIndex:"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsinvocation/getargument:atindex:"
---

# getArgument:atIndex:

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

## Declaration

```occ
- (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

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. warning: NSInvocation copies the return value directly into buffer without any memory bookkeeping, even when using Automatic Reference Counting (ARC). When using ARC, pass a pointer whose memory semantics match those expected by the method. Otherwise, an over-release crash may occur. For typical Objective-C methods, where ownership isn’t returned to the caller, use an unsafe_unretained variable. You can then copy this into another form of storage, like a strongly-held property. The following example shows how to properly manage memory when using ARC. // 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

- [numberOfArguments](foundation/nsmethodsignature/numberofarguments.md)

### Configuring an Invocation Object

- [selector](foundation/nsinvocation/selector.md)
- [target](foundation/nsinvocation/target.md)
- [setArgument:atIndex:](foundation/nsinvocation/setargument:atindex:.md)
- [argumentsRetained](foundation/nsinvocation/argumentsretained.md)
- [retainArguments](foundation/nsinvocation/retainarguments.md)
- [setReturnValue:](foundation/nsinvocation/setreturnvalue:.md)
- [getReturnValue:](foundation/nsinvocation/getreturnvalue:.md)
