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

 
 
--EOF--
作者:Buro#79xxd 出处:http://www.cnblogs.com/buro79xxd/ 文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Objective-C中一种消息处理方法performSelector: withObject:的更多相关文章

  1. 【转】Objective-C中一种消息处理方法performSelector: withObject:

    原文 : http://www.cnblogs.com/buro79xxd/archive/2012/04/10/2440074.html   Objective-C中调用函数的方法是“消息传递”,这 ...

  2. HTTP协议以及HTTP请求中8种请求方法

    HTTP协议以及HTTP请求中8种请求方法 什么是协议? 协议,是指通信的双方,在通信流程或内容格式上,共同遵守的标准. 什么是http协议? http协议,是互联网中最常见的网络通信标准. http ...

  3. ASP.NET中几种加密方法

    下面就是ASP.NET中几种加密方法.加密算法有两种,也就是上面提到的MD5和SHA1,这里我举的例子是以MD5为例,SHA1大致相同,只是使用的类不一样. MD5的全称是Message-Digest ...

  4. iOS中 三种随机数方法详解

    ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...

  5. HTTP请求方式中8种请求方法(简单介绍)

    简单介绍 HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范.HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口是动态分配的.当我们没有指定端口访问时,浏览器会 ...

  6. DELPHI中的消息处理机制(三种消息处理方法的比较,如何截断消息)

    DELPHI中的消息处理机制 Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpasca ...

  7. c++中两种常量方法的比较

    [c++]在C++中定义常量的两种方法的比较   常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如: #define PRICE 10 ...

  8. Java Web 项目发布到Tomcat中三种部署方法

    第一种方法:在tomcat中的conf目录中,在server.xml中的,节点中添加: <Context path="/" docBase="E:\TOMCAT\a ...

  9. 转:Java Web 项目发布到Tomcat中三种部署方法

    首先整理项目文件,文件内包含jsp.js等和class编译后的文件及lib包,如: 第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加:   ...

随机推荐

  1. 使用Eclipse调试Android Native Application---cocos2d-x + Eclipse + Android + ndk

    纠结很多天的ndk 调试, 终于在 mac  下面顺利完成(注意在windows还是没弄成功,蛋疼...) 调试方法: 1:先google, ndk demo .  把ndk 最基本的hellword ...

  2. NIS 服务器

    有没有想过,如果我有十部 Linux 主机,这十部主机仅负责不同的功能,事实上, 所有的主机账号与对应的密码都相同!那么我是将账号与密码分别设定置在十部计算机上面, 还是可以透过一部主机做为账号管理的 ...

  3. ORACLE查看锁(lock)情况

      SELECT /*+ RULE */         ls.osuser os_user_name,          ls.username user_name,          DECODE ...

  4. phpMyAdmin 'import.php'跨站脚本漏洞

    漏洞版本: phpMyAdmin phpMyAdmin 3.4.9 phpMyAdmin phpMyAdmin 3.4.8 phpMyAdmin phpMyAdmin 3.4.6 phpMyAdmin ...

  5. puppy 制作linux

    经过一段时间的使用以后,我们每个人电脑里的Puppy Linux都是独一无二的,我们可以通过简单的方法将自己电脑上的Puppy制作成iso或Live-CD,成为自己玩的“Only You”Puppy ...

  6. POJ 1321 棋盘问题(DFS & 状压DP)

    用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...

  7. xfce terminal tab被解释为super+tab的bug

    https://bugzilla.xfce.org/show_bug.cgi?id=10760 解决方法 Every few weeks the tab key stops working in th ...

  8. 保持与 Microsoft Azure Files 的连接

    我们在最近的博客文章中介绍了 Azure StorageFiles的预览版,请单击此处.该文章包含 Azure Files 的相关信息,说明了如何申请预览版并开始使用,还介绍了一些有助于创建共享和传 ...

  9. JS中document.createElement()用法及注意事项

    今天处理了一个日期选择器的ie和ff的兼容问题,本来这种情况就很难找错误,找了好久才把错误定位到js中创建元素的方法document.createElement(),这个方法在ie下支持这样创建元素 ...

  10. HDU 1953

    #include<stdio.h> #include<math.h> long long int euler(long long int n) { long long int ...