NSInvoaction的用法
来源:互联网 发布:number是什么软件 编辑:程序博客网 时间:2024/05/22 02:04
NSInvocation
类的实例用于封装Objective-C消息。一个调用对象中含有一个目标对象(target)、一个方法选择器(selector)、以及方法参数(arguments)。您可以动态地改变调用对象中消息的目标及其参数,一旦消息被执行,您就可以从该对象得到返回值(getReturnValue:)。
唯一的初始化方法:
+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)sig创建
NSInvocation
对象需要使用NSMethodSignature
对象,该对象负责封装与方法参数和返回值有关系的信息。NSMethodSignature
对象的创建又需要用到一个方法选择器。NSInvocation
的实现还用到Objective-C运行环境的一些函数。- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector;
假设Man对象
@interface Man : NSObject- (NSString *)invocationWithPar:(NSString *)parOne parTwo:(NSString *)parTwo parThree:(NSString *)parThree;@end<pre name="code" class="objc">- (NSString *)invocationWithPar:(NSString *)parOne parTwo:(NSString *)parTwo parThree:(NSString *)parThree{ return [NSString stringWithFormat:@"%@-%@-%@",parOne,parTwo,parThree];}
Man *man = [[Man alloc] init]; NSString *milk = @"strOne"; NSString *fruit = @"strTwo"; NSString *food = @"strThree"; NSString *resultStr; SEL sel = @selector(invocationWithPar:parTwo:parThree:); NSMethodSignature *sig = [Man instanceMethodSignatureForSelector:sel];//初始化NSMethodSignature if (sig) { //用NSMethodSignatur初始化NSInvocation NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig]; //设置selector---调用的方法 invocation.selector = sel; //设置target---调用的对象 invocation.target = man; //设置参数--从第2的位置开始,0和1分别为target和selector [invocation setArgument:&milk atIndex:2]; [invocation setArgument:&fruit atIndex:3]; [invocation setArgument:&food atIndex:4]; [invocation retainArguments]; [invocation invoke]; //设置返回结果的地址 [invocation getReturnValue:&resultStr]; NSLog(@"resultStr = %@",resultStr); }NSInvocation 主要应用于运行时的消息转发,多此发送消息等。
0 0
- NSInvoaction的用法
- ?的用法
- ?的用法
- ---" ~ " 的用法---
- ---" ~ " 的用法---
- ~ 的用法
- 的用法
- ##的用法
- ##的用法
- 的用法
- ${ }的用法
- ##的用法
- ${ }的用法
- "->"的用法
- ${ }的用法
- >>的用法
- &的用法
- ${}的用法
- Codeforces 495 B. Modular Equations && Codeforces Round #282 (Div. 2)
- 2014微软校园招聘笔试试题(英文)
- 如何将当前界面的可视组件以同样的相对位置和大小保存在png图像文件中?要求写出实践的代码。
- bzoj1588 [HNOI2002]营业额统计 (Splay)
- 常见错误及解决方法
- NSInvoaction的用法
- ExQuilla 23 and Thunderbird 17: Ubuntu 下免费使用Microsoft Exchange Server
- java中文参数乱码问题
- uva1368 字符串构造
- 神经网络入门
- 二叉树的有关操作
- Codeforces 494 A. Treasure && Codeforces Round #282 (Div. 1)
- java反射学习
- linux-usb驱动开发一