IOS NSInvocation用法简介
IOS NSInvocation用法简介
[摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是NSInvocation,本文介绍IOS NSInvocation用法,并提供简单的示例代码供参考。
在 iOS中可以直接调用某个对象的消息方式有两种:
一种是performSelector:withObject;
再一种就是NSInvocation。
第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作。
main.h
#import <Foundation/Foundation.h>
#import "MyClass.h" int main (int argc, const char * argv[])
{ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; MyClass *myClass = [[MyClass alloc] init];
NSString *myString = @"My string"; //普通调用
NSString *normalInvokeString = [myClass appendMyString:myString];
NSLog(@"The normal invoke string is: %@", normalInvokeString); //NSInvocation调用
SEL mySelector = @selector(appendMyString:);
NSMethodSignature * sig = [[myClass class]
instanceMethodSignatureForSelector: mySelector]; NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];
[myInvocation setTarget: myClass];
[myInvocation setSelector: mySelector]; [myInvocation setArgument: &myString atIndex: 2]; NSString * result = nil;
[myInvocation retainArguments];
[myInvocation invoke];
[myInvocation getReturnValue: &result];
NSLog(@"The NSInvocation invoke string is: %@", result); [myClass release]; [pool drain];
return 0;
}
MyClass.h
1 | #import <Foundation/Foundation.h> |
2 | |
3 | |
4 | @interface MyClass : NSObject { |
5 | } |
6 | |
7 | - (NSString *)appendMyString:(NSString *)string; |
8 | |
9 | @end |
MyClass.m
1 | #import "MyClass.h" |
2 | |
3 | |
4 | @implementation MyClass |
5 | |
6 | - (id)init |
7 | { |
8 | self = [super init]; |
9 | if (self) { |
10 | // Initialization code here. |
11 | } |
12 | |
13 | return self; |
14 | } |
15 | |
16 | - (NSString *)appendMyString:(NSString *)string |
17 | { |
18 | NSString *mString = [NSString stringWithFormat:@"%@ after append method", string]; |
19 | |
20 | return mString; |
21 | } |
22 | |
23 | - (void)dealloc |
24 | { |
25 | [super dealloc]; |
26 | } |
27 | |
28 | @end |
这里说明一下[myInvocation setArgument: &myString atIndex: 2];为什么index从2开始 ,原因为:0 1 两个参数已经被target 和selector占用。
-------------以下为我对这三个方法的理解
[myInvocation retainArguments];//如果方法还未执行,retain该object对象和方法下的所有参数对象
[myInvocation invoke]; //执行方法
[myInvocation getReturnValue: &result];//得到返回值
IOS NSInvocation用法简介的更多相关文章
- iOS系统版本简介
iOS系统版本简介 ⽬目前iOS设备所⽀支持的最主流操作系统是iOS6,⼤大概占了93%,⽽而使 ⽤用iOS5的iOS设备⼤大概占6%,剩下的只有1%.( 根据苹果的官⽅方数据 ) 从iOS1到现在的 ...
- JodaTime用法简介
JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...
- Apache自带压力测试工具ab用法简介
ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...
- Postman用法简介
转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...
- MSSQL Sql加密函数 hashbytes 用法简介
转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...
- java assert的用法简介【转】
assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...
- glVertexAttribPointer 用法简介
在内存中采用交叉模式存储,向gpu传入顶点数据的方法 GPU: #version 100 attribute highp vec2 aPosition; attribute highp vec2 a ...
- C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介
C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介 IP是一种普遍应用于因特网.允许不同主机能够相互找到对方的寻址协议.IP地址由4个十进制的数字号码所组成, ...
- Postman用法简介----https://blog.csdn.net/flowerspring/article/details/52774399
https://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介
随机推荐
- Ramdisk文件系统无法启动
当kernel是使用ramdisk时,bootm命令有两种使用方式: 1.bootm ${kernel_addr} 此种方式要求bootargs变量包含ramdisk的地址和大小,ramdisk的格式 ...
- Intellij IDEA中部署Tomcat报错“war exploded: Server is not connected. Deploy is not available”
最近将本机的Tomcat 7卸载重装了Tomcat 9后,在IDEA中运行Tomcat老是报错,一直未找到解决问题的办法. 只能讲Tomcat重新退回到版本7,才重新再IDEA中运行正常.
- css的两种盒子模型
css的两种盒子模型:W3C标准盒子模型.IE盒子模型 两者的相同之处:都包含margin.border.padding.content 两者的不同之处:W3C标准盒子模型的content部分不包含其 ...
- tsne降维可视化
Python代码:准备训练样本的数据和标签:train_X4000.txt.train_y4000.txt 放于tsne.py当前目录.(具体t-SNE – Laurens van der Maate ...
- 如何判断一个Div是否可视区域,判断div是否可见
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery学习小结2——动画
一.基础动画 方法名 说明 show([speed,[easing],[fn]])hide([speed,[easing],[fn]]) speed:三种预定速度之一的字符串("slow&q ...
- linux win 的换行转换
Linux和Windows和换行符不一样.Windows下是CRLF(\r\n或0d0a),Linux下是LF(\n或0a).在Linux下有时会遇到从Windows过来的文本文件,这些文件带了Win ...
- Cookie实例,理解cookie
一.一句话了解cookie是什么 cookie是服务端发送给客户端的.用来记录一些信息(如用户名),定制主页,聚焦广告的.最终以文件形式存在于客户端电脑磁盘下的小型文档. 二.用实例来认清cookie ...
- myeclipse 6.5配置tomcat7.X
软件安装:myeclipse 6.5 Apache Tomcat/7.0.47 安装完成后开始配置 1. 随便展开一个tomcat配置选项,这里配置到tomcat6.x,如下图:
- js控制html文字提示语的出现和隐藏
有时我们需要在点击html输入框的时候,旁边会出现提示语.在输入字符的时候,输入框下边会出现输入了多少字符的提示. 请看下面实例. <!DOCTYPE html> <html> ...