您的位置:首页 > 移动开发 > IOS开发

iOS调用perform selector超过2个参数以上的方法

2016-06-12 10:18 441 查看
Cocoa内置只支持两个参数,要超过两个参数以上怎么办呢,下面代码展示了如何实现一个自己的方法来调用超过7个参数(来自three20)


- (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3
    withObject:(id)p4 withObject:(id)p5 withObject:(id)p6 withObject:(id)p7
{
 
 NSMethodSignature *sig = [self methodSignatureForSelector:selector];
 
 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 setArgument:&p4 atIndex:5];
 
   [invo setArgument:&p5 atIndex:6];
 
  [invo setArgument:&p6 atIndex:7];
 
   [invo setArgument:&p7 atIndex:8];
   
 [invo invoke];
  
  if (sig.methodReturnLength) {
   
   id anObject;
     
 [invo getReturnValue:&anObject];
     
 return anObject;
    } 
else {
      return nil;
    }
  } 
else {
    return nil;
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: