ios如何用perform selector调用超过两个以上参数的方法


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

你可能感兴趣的:(ios,cocoa)