<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSInvocation/getArgument:atIndex:",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSInvocation(im)getArgument:atIndex:"
  },
  "title" : "getArgument:atIndex:"
}
-->

# getArgument:atIndex:

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

```
- (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`](/documentation/Foundation/NSMutableString) and an `int`. The example performs the invocation with [`invoke`](/documentation/Foundation/NSInvocation/invoke), then retrieves the first argument with [`getArgument:atIndex:`](/documentation/Foundation/NSInvocation/getArgument:atIndex:) and copies it to a strongly-held property called `myObject`.

> Warning:
> ``doc://com.apple.foundation/documentation/Foundation/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.

```objc
// 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`](/documentation/Foundation/NSExceptionName/invalidArgumentException) if `index` is greater than the actual number of arguments for the selector.

## See Also

[`@property (readonly) NSUInteger numberOfArguments;`](/documentation/Foundation/NSMethodSignature/numberOfArguments)

The number of arguments recorded in the receiver.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)