函数做参数传递

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;
}
}

posted on 2012-12-08 11:02  千手人屠  阅读(162)  评论(0)    收藏  举报

导航