【转】Objective-C中一种消息处理方法performSelector: withObject:
原文 : http://www.cnblogs.com/buro79xxd/archive/2012/04/10/2440074.html
Objective-C中调用函数的方法是“消息传递”,这个和普通的函数调用的区别是,你可以随时对一个对象传递任何消息,而不需要在编译的时候声明这些方法。所以Objective-C可以在runtime的时候传递人和消息。
首先介绍两个方法 SEL和@selector
根据AppleObjective-C Runtime Reference官方文档这个传递消息的函数就是 id objc_msgSend(id theReceiver, SEL theSelector, …)
theReceiver是接受消息的对象类型是id,theSelector是消息名称类型是SEL。下边代码我们来看看如何来生成一个SEL,如果传递消息。
- (void) fooNoInputs {
NSLog(@"Does nothing");
}
然后调用它
[self performSelector:@selector(fooNoInputs)];
第二个试验看看如何在消息中传递参数
我们建立一个有input参数的函数
- (void) fooOneIput:(NSString*) first {
NSLog(@"Logs %@", first);
}
然后调用它
[self performSelector:@selector(fooOneInput:) withObject:@"first"];
第三个试验更多的参数
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second {
NSLog(@"Logs %@ then %@", first, second);
}
然后调用它
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second"];
第四个试验如何建立动态的函数,然后调用他们?我们需要建立一个selector
SEL myTestSelector = @selector(myTest:);
并且我们调用的函数在另外一个Class内
- (void)abcWithAAA: (NSNumber *)number {
int primaryKey = [number intValue];
NSLog("%i", primaryKey);
}
MethodForSelectors * mfs = [[MethodForSelectors alloc]init];
NSArray *Arrays = [NSArray arrayWithObjects:@"AAA", @"BBB", nil];
for ( NSString *array in Arrays ){
SEL customSelector = NSSelectorFromString([NSStringstringWithFormat:@"abcWith%@:", array]);
mfs = [[MethodForSelectors alloc] performSelector:customSelector withObject:0];
}
注意:updated at 20120606
1.如果使用了ARC会产生“performSelector may cause a leak because its selector is unknown”警告
2.这种方式当传入一个不符合约定的消息时会继续运行并不报错。例如应该传入2个参数,但只传入1个参数。或传入了3个参数,第三个参数不会被初始化。还有一种调用其他Class Function的方法是,但是不能有参数,我们这里假设没有参数,那么就可以这样
[mfs customSelector];
完整的代码:
@implementation ClassForSelectors
- (void) fooNoInputs {NSLog(@"Does nothing");
}
- (void) fooOneIput:(NSString*) first {
NSLog(@"Logs %@", first);
}
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second {
NSLog(@"Logs %@ then %@", first, second);
}
- (NSArray *)abcWithAAA: (NSNumber *)number {
int primaryKey = [number intValue];
NSLog("%i", primaryKey);
}
- (void) performMethodsViaSelectors {
[self performSelector:@selector(fooNoInputs)];
[self performSelector:@selector(fooOneInput:) withObject:@"first"];
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second"];
}
- (void) performDynamicMethodsViaSelectors {
MethodForSelectors * mfs = [MethodForSelectors alloc];
NSArray *Arrays = [NSArray arrayWithObjects:@"AAA", @"BBB", nil];
for ( NSString *array in Arrays ){
SEL customSelector = NSSelectorFromString([NSStringstringWithFormat:@"abcWith%@:", array]);
mfs = [[MethodForSelectors alloc] performSelector:customSelector withObject:0];
}
}
@end
@implementation MethodForSelectors- (void)abcWithAAA: (NSNumber *)number {
NSLog("%i", number);
}
@end
【转】Objective-C中一种消息处理方法performSelector: withObject:的更多相关文章
- Objective-C中一种消息处理方法performSelector: withObject:
Objective-C中调用函数的方法是“消息传递”,这个和普通的函数调用的区别是,你可以随时对一个对象传递任何消息,而不需要在编译的时候声明这些方法.所以Objective-C可以在runtime的 ...
- HTTP协议以及HTTP请求中8种请求方法
HTTP协议以及HTTP请求中8种请求方法 什么是协议? 协议,是指通信的双方,在通信流程或内容格式上,共同遵守的标准. 什么是http协议? http协议,是互联网中最常见的网络通信标准. http ...
- ASP.NET中几种加密方法
下面就是ASP.NET中几种加密方法.加密算法有两种,也就是上面提到的MD5和SHA1,这里我举的例子是以MD5为例,SHA1大致相同,只是使用的类不一样. MD5的全称是Message-Digest ...
- iOS中 三种随机数方法详解
ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...
- HTTP请求方式中8种请求方法(简单介绍)
简单介绍 HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范.HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口是动态分配的.当我们没有指定端口访问时,浏览器会 ...
- DELPHI中的消息处理机制(三种消息处理方法的比较,如何截断消息)
DELPHI中的消息处理机制 Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpasca ...
- c++中两种常量方法的比较
[c++]在C++中定义常量的两种方法的比较 常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如: #define PRICE 10 ...
- Java Web 项目发布到Tomcat中三种部署方法
第一种方法:在tomcat中的conf目录中,在server.xml中的,节点中添加: <Context path="/" docBase="E:\TOMCAT\a ...
- 转:Java Web 项目发布到Tomcat中三种部署方法
首先整理项目文件,文件内包含jsp.js等和class编译后的文件及lib包,如: 第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加: ...
随机推荐
- Polyfills
填充物..github上比较全的列表 IE总是有填不完的坑,在此总结一下,可能暂时都是一知半解,错了我会慢慢改的. 首先html5标签需要填充一些class,可以用html5.js处理,或者html5 ...
- POJ 1651 Multiplication PuzzleDP方法:
有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个分数,分值的计算方法是:要拿的卡片,和它左右两边的卡片,这三张卡片上数字 ...
- C#怎么得到主机名,IP,MAC
一:基础知识 a: Dns 类 提供简单的域名解析功能. Dns 类是一个静态类,它从 Internet 域名系统 (DNS) 检索关于特定主机的信息. 在 IPHostEntry 类的实例中返回来自 ...
- android.process.acore和system进程
从源码看来,android.process.acore进程应该是一些基本功能的载入程序. android-4.3_r2.2中,它包括以下项目: 1.UserDictionaryProvider < ...
- codevs3945 完美拓印
3945 完美拓印 codevs月赛 第一场 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 小Q获得了一个神奇的印章,这个印章宽n ...
- 【转】Linux下查看所有用户及用户组
groups 查看当前登录用户的组内成员groups gliethttp 查看gliethttp用户所在的组,以及组内成员whoami 查看当前登录用户名/etc/group文件包含所有组/etc/s ...
- maven打一个可执行的jar包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-depen ...
- Java读书笔记二(封装类)
1.介绍 都知道java中基本数据类型有非常多,比方string,int--,可是基本数据类型与对象之间是不同的.但非常多情况下,我们希望将基本数据类型当作对象使用,这时候就须要用到封装类. 2.封装 ...
- PHP自学之路---报表及绘图技术
Ø 报表 a) 什么是报表 报表就是用表格.图表等格式来动态显示数据. b) PHP绘图坐标系 1. 坐标系介绍 下图说明了PHP坐标系.坐标原点位于左上角,以像素为单 ...
- 动态规划晋级——POJ 3254 Corn Fields【状压DP】
转载请注明出处:http://blog.csdn.net/a1dark 分析:刚开始学状压DP比较困难.多看看就发现其实也没有想象中那么难.这道题由于列数较小.所以将行压缩成二进制来看.首先处理第一行 ...