Saturday, May 11, 2013

NSInvocation setReturnValue example ios


setReturnValue:

Sets the receiver’s return value.
- (void)setReturnValue:(void *)buffer
Parameters
buffer
An untyped buffer whose contents are copied as the receiver's return value.
Discussion( NSInvocation setReturnValue example )
This value is normally set when you send an invoke or invokeWithTarget: message.
( NSInvocation setReturnValue example )
void (^theBlock)(NSInvocation *) = ^(NSInvocation *invocation) {
    NSString *capitalizedString = @"255";
    [invocation setReturnValue:&capitalizedString];
};
( NSInvocation setReturnValue example )
- (void)setUpReturnValue:(NSInvocation *)anInvocation
{
  if (returnValueIsFromInvocation) {
    NSInvocation *returnValueInvocation = (NSInvocation *)returnValue;
    [returnValueInvocation invoke];
    void *buffer = malloc([[anInvocation methodSignature] methodReturnLength]);
    [returnValueInvocation getValue:buffer];
    [anInvocation setReturnValue :buffer];
    free(buffer);
  }
  else if(returnValueShouldBeThrown)
  {
    @throw returnValue;
  }
  else if(returnValueIsBoxed)
  {
    if(strcmp([[anInvocation methodSignature] methodReturnType],
              [(NSValue *)returnValue objCType]) != 0)
      [NSException raise:NSInvalidArgumentException
                  format:@"Return value does not match method signature."];
    void *buffer = malloc([[anInvocation methodSignature] methodReturnLength]);
    [returnValue getValue:buffer];
    [anInvocation setReturnValue :buffer];
    free(buffer);
  }
  else
  {
    const char *returnType = [[anInvocation methodSignature] methodReturnType];
    const char *returnTypeWithoutQualifiers = returnType + (strlen(returnType) - 1);
    if(strcmp(returnTypeWithoutQualifiers, @encode(id)) == 0)
      [anInvocation setReturnValue:&returnValue];   
  }
}