动态调用方法Use the NSInvocation class

 
 

Use the NSInvocation class, like so:
- (NSString *) myMethod:(NSString *)param1

withParam2:(NSNumber *)param2

{

NSString *result = @"Objective-C";

NSLog(@"Param 1 = %@", param1);NSLog(@"Param 2 = %@", param2);

return(result);

}

- (void) invokeMyMethodDynamically

{
SEL selector = @selector(myMethod:withParam2:);

NSMethodSignature *methodSignature =
[[self class] instanceMethodSignatureForSelector:selector];

NSInvocation *invocation =

[NSInvocation invocationWithMethodSignature:methodSignature];[invocation setTarget:self];
[invocation
setSelector:selector];

NSString *returnValue = nil;
NSString *
argument1 = @"First Parameter";
NSNumber *
argument2 = [NSNumber numberWithInt:102];

[invocation setArgument:&argument1atIndex:2];

[invocation setArgument:&argument2atIndex:3];

[invocation retainArguments];
[invocation
invoke];
[invocation
getReturnValue:&returnValue];

NSLog(@"Return Value = %@", returnValue);

}


猜你喜欢

转载自blog.csdn.net/lanliang901125/article/details/18657263