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

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

概述Cocoa内置只支持两个参数,要超过两个参数以上怎么办呢,下面代码展示了如何实现一个自己的方法调用超过7个参数(来自three20) 

- (id)performSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 withObject:(id)p3
    withObject:(id)p4 withObject:(id)p5

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如何用perform selector调用超过两个以上参数的方法全部内容,希望文章能够帮你解决ios如何用perform selector调用超过两个以上参数的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066405.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存