函数做参数传递
http://www.cnblogs.com/scorpiozj/archive/2011/05/17/2048751.html
- (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3 {
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
//记录方法的参数和返回值类型,通常用于对象间传递消息;通常随后会创建NSInvocation对象,来调用方法
if (sig) {
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];//对象间信息存储和传递
[invo setTarget:self];
[invo setSelector:selector];
[invo setArgument:&p1 atIndex:2];//注意索引号
[invo setArgument:&p2 atIndex:3];
[invo setArgument:&p3 atIndex:4];
[invo invoke];//调用函数,判断是否有返回值
if (sig.methodReturnLength) {
id anObject;
[invo getReturnValue:&anObject];
return anObject;
} else {
return nil;
}
} else {
return nil;
}
}
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
//记录方法的参数和返回值类型,通常用于对象间传递消息;通常随后会创建NSInvocation对象,来调用方法
if (sig) {
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];//对象间信息存储和传递
[invo setTarget:self];
[invo setSelector:selector];
[invo setArgument:&p1 atIndex:2];//注意索引号
[invo setArgument:&p2 atIndex:3];
[invo setArgument:&p3 atIndex:4];
[invo invoke];//调用函数,判断是否有返回值
if (sig.methodReturnLength) {
id anObject;
[invo getReturnValue:&anObject];
return anObject;
} else {
return nil;
}
} else {
return nil;
}
}

浙公网安备 33010602011771号