记录Objective-C里SEL和Selector的用法
来源:互联网 发布:淘宝康泽大药房 编辑:程序博客网 时间:2024/06/05 17:37
Objective-C中调用函数的方法是“消息传递”,这个和普通的函数调用的区别是,你可以随时对一个对象传递任何消息,而不需要在编译的时候声明这些方法。所以Objective-C可以在runtime的时候传递人和消息。
首先介绍两个方法 SEL和@selector
根据AppleObjective-C Runtime Reference官方文档这个传递消息的函数就是 id objc_msgSend(idtheReceiver, 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];
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];
}
完整的代码:
@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/文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
来源:http://www.th7.cn/Program/IOS/2012/04/10/69094.shtml
- 记录Objective-C里SEL和Selector的用法
- 记录Objective-C里SEL和Selector的用法
- Objective-c selector/SEL 说明
- Objective-C中Selector基本概念和操作(SEL类型)
- Objective-C学习笔记之SEL和@selector
- Objective-C中的SEL的用法
- [Objective-C]SEL类型、@selector选择器
- [Objective-C]SEL类型、@selector选择器
- Objective-C - SEL (@selector) 原理及本质
- [Objective-C]SEL类型、@selector选择器
- OC学习:SEL类型数据和selector的用法
- Objective C的SEL类型
- SEL和selector的简单使用
- iOS: Objective C "Selector" 用法
- SEL 和 @selector
- Objective-C中的SEL
- Objective-C中的SEL .
- objective-c中的SEL
- 深圳面试总结
- 学会接受不同的意见
- JFreeChart生成柱状图及其在struts2下的展现
- hdu 2874 Connections between cities LCA
- 【Jquery Autocomplete】参数配置资料
- 记录Objective-C里SEL和Selector的用法
- Unity3D 3.5 之windows平台coding及debug环境小结
- DB2存储过程学习笔记(一)--DROP DELETE TRUNCATE 性能
- 蒙特卡洛光线追踪
- ios局域网联机——苹果官方源码之WiTap剖析
- WebDeploy Error - The response header 'MSDeploy.Response' was '' but 'v1' was expected.
- Sed学习笔记
- USACO section 1.3.4 Prime Cryptarithm
- 通过Oracle Stream实现数据库之间的同步