---
title: "getReturnValue:"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsinvocation/getreturnvalue:"
---

# getReturnValue:

Gets the invocation’s return value.

## Declaration

```occ
- (void) getReturnValue:(void *) retLoc;
```

## Parameters

- `retLoc`: An untyped buffer into which the invocation copies its return value. It should be large enough to accommodate the value. See the discussion below for more information about buffer.

## Discussion

Discussion Use the NSMethodSignature method methodReturnLength to determine the size needed for buffer: NSUInteger length = [[myInvocation methodSignature] methodReturnLength]; buffer = (void *)malloc(length); [invocation getReturnValue:buffer]; When the return value is an object, pass a pointer to the variable (or memory) into which NSInvocation should place the object. In the following example, myInvocation represents a call to a no-argument method called createReturnValue in a class called MyClass, which returns an NSMutableString. The example performs the invocation with invoke, then retrieves the object with getReturnValue: 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. However, if the only thing keeping the returned value alive is the invocation’s target, then you must keep the target alive while using the returned value. You can do this with the macro NS_VALID_UNTIL_END_OF_SCOPE on the target. The following example shows how to properly manage memory when using ARC. // Create invocation, set selector, and invoke. NS_VALID_UNTIL_END_OF_SCOPE MyClass* myTarget = [[MyClass alloc] init]; NSMethodSignature* mySignature = [MyClass     instanceMethodSignatureForSelector: @selector(createReturnValue)]; NSInvocation* myInvocation = [NSInvocation invocationWithMethodSignature: mySignature]; [myInvocation setSelector: @selector(createReturnValue)];

[myInvocation invokeWithTarget: myTarget];

// Retrieve return value and assign to property. __unsafe_unretained id tempObject = nil; [myInvocation getReturnValue: &tempObject]; self.myObject = tempObject; If you haven’t invoked the NSInvocation object, the result of this method is undefined.

## See Also

### Related Documentation

- [methodReturnType](foundation/nsmethodsignature/methodreturntype.md)

### Configuring an Invocation Object

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