NSInvocation使用示例 IOS 对象方法的调用 NSInvocation PK performSelector

http://blog.csdn.net/yhawaii/article/details/8306637

一、概述

在 iOS中可以直接调用 某个对象的消息 方式有2种


第一种方式是使用NSObject类提供的performSelector系列方法


还有一种方式就是使用NSInvocation进行动态运行时的消息分发,动态的执行方法,相信大家一定经常使用NSObject类提供的performSelector系列方法,在这里就不再对此进行描述了,今天主要是分享一下使用NSInvocation动态执行方法。


二、NSInvocation的使用

1、执行类方法

demo代码如下:

[cpp]  view plain  copy
  1. - (void)testClassMethod{  
  2.     NSString *string = nil;  
  3.       
  4.     //初始化NSMethodSignature对象  
  5.     NSMethodSignature *sig = [NSString methodSignatureForSelector:@selector(stringWithString:)];  
  6.       
  7.     //初始化NSInvocation对象  
  8.     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];  
  9.       
  10.     //设置执行目标对象  
  11.     [invocation setTarget:[NSString class]];  
  12.       
  13.     //设置执行的selector  
  14.     [invocation setSelector:@selector(stringWithString:)];  
  15.       
  16.     //设置参数  
  17.     NSString *argString = @"test method";  
  18.     [invocation setArgument:&argString atIndex:2];  
  19.       
  20.     //执行方法  
  21.     [invocation retainArguments];  
  22.     [invocation invoke];  
  23.       
  24.     //获取返回值  
  25.     [invocation getReturnValue:&string];  
  26.       
  27.     NSLog(@"执行结果 ====%@",string);  
  28. }  


2、执行实例方法

demo示例代码如下:
[cpp]  view plain  copy
  1. - (void)testInstanceMethod{  
  2.     NSString *string = [NSString stringWithFormat:@"我是一个string"];  
  3.     NSLog(@"1=%@",string);  
  4.     SEL subStringSel = @selector(substringFromIndex:);  
  5.       
  6.     //初始化NSMethodSignature对象  
  7.     NSMethodSignature *methodSignature = [[NSString class] instanceMethodSignatureForSelector:subStringSel];  
  8.       
  9.     //初始化NSInvocation对象  
  10.     NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:methodSignature];  
  11.       
  12.     //设置target  
  13.     [myInvocation setTarget:string];  
  14.       
  15.     //设置selector  
  16.     [myInvocation setSelector:subStringSel];  
  17.       
  18.     //设置参数  
  19.     int arg1 =  2;  
  20.     [myInvocation setArgument:&arg1 atIndex:2];//参数从2开始,index 为0表示target,1为_cmd  
  21.       
  22.     //获取结果  
  23.     NSString *resultString = nil;  
  24.     [myInvocation invoke];  
  25.     [myInvocation getReturnValue:&resultString];  
  26.     NSLog(@"2=%@",resultString);  
  27. }  

http://blog.sina.com.cn/s/blog_74e9d98d0101eekc.html

IOS 对象方法的调用  NSInvocation  PK  performSelector

网上有讨论NSInvocation的用法,说某些情况下performSelector不是很方便:

在 iOS中可以直接调用 某个对象的消息 方式有2

一种是performSelector:withObject:

再一种就是NSInvocation

第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作


个人觉得不以为然,返回值,采用performSelector也能获取返回值,不过返回值都是id类型
对于多个参数的问题,可以采用压箱操作,即把多个参数用NSDictionary 封装成一个对象再传入,这就需要你能随意的修改目标类中的这个方法了,不然也只能采用NSInvocation


地址:http://tiny4cocoa.com/thread-1963-1-1.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值