iOS调用方法---NSInvocation的使用
来源:互联网 发布:mac appstore更新不了 编辑:程序博客网 时间:2024/05/20 13:09
在 iOS中可以直接调用 某个对象的消息 方式有2种
一种是performSelector:withObject:
再一种就是NSInvocation, invocation英文单词就是调用的意思
第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作
NSInvocation可以处理参数、返回值。会java的人都知道反射操作,其实NSInvocation就相当于反射操作。
下面这个例子描述了如何使用NSInvocation,以下例子中如果要正常运行,需要把不存在的类进行正确填写。
SEL subStringSel = @selector(substringFromIndex:); //初始化NSMethodSignature对象 NSMethodSignature *methodSignature = [[NSString class] instanceMethodSignatureForSelector:subStringSel]; //初始化NSInvocation对象 NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:methodSignature]; //设置target [myInvocation setTarget:string]; //设置selector [myInvocation setSelector:subStringSel]; //设置参数 int arg1 = 2; [myInvocation setArgument:&arg1 atIndex:2];//参数从2开始,index 为0表示target,1为_cmd //获取结果 NSString *resultString = nil; [myInvocation invoke]; [myInvocation getReturnValue:&resultString]; NSLog(@"2=%@",resultString);
- (void)testClassMethod{ NSString *string = nil; //初始化NSMethodSignature对象 NSMethodSignature *sig = [NSString methodSignatureForSelector:@selector(stringWithString:)]; //初始化NSInvocation对象 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig]; //设置执行目标对象 [invocation setTarget:[NSString class]]; //设置执行的selector [invocation setSelector:@selector(stringWithString:)]; //设置参数 NSString *argString = @"test method"; [invocation setArgument:&argString atIndex:2]; //执行方法 [invocation retainArguments]; [invocation invoke]; //获取返回值 [invocation getReturnValue:&string]; NSLog(@"执行结果 ====%@",string); }
0 0
- iOS调用方法---NSInvocation的使用
- NSInvocation使用示例 IOS 对象方法的调用 NSInvocation PK performSelector
- iOS NSInvocation的使用
- iOS NSInvocation的使用
- NSInvocation简单使用 调用方法
- iOS 类的实例方法调用 NSInvocation performSelector
- iOS中NSInvocation的使用
- [iOS] NSInvocation简单使用
- [iOS] NSInvocation简单使用
- ios NSInvocation简单使用
- [iOS] NSInvocation简单使用
- ios NSInvocation简单使用
- iOS NSInvocation 使用
- 使用NSInvocation 调用实例的方法,且等待获得返回值
- 使用NSInvocation 调用实例的方法,且等待获得返回值
- iOS NSInvocation的学习
- NSInvocation的使用
- NSInvocation的使用
- 高次同余方程:poj 3243+poj 2417+hdu 2815 (Baby Step Giant Step 算法)
- 内存管理及优化
- Skiing
- unity3d 异步socket packet序列化
- android usb挂载分析--ntfs-3g移植
- iOS调用方法---NSInvocation的使用
- android 适配深入研究
- php实现解析网页并下载图片到本地
- 命名空间学习笔记
- 1.Foundation概述
- Java书籍推荐
- 对编码人员的几个期望
- 自定义日历控件-CalendarView
- 第21、22讲 链表、枚举 项目三:应用举例(输出对称点)