Contents

getReturnValue:

Gets the invocation’s return value.

Declaration

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

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.

// 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

Configuring an Invocation Object