Objective-C中一种消息处理方法performSelector: withObject:
来源:互联网 发布:淘宝宝贝网址打不开 编辑:程序博客网 时间:2024/05/21 17:19
转载地址http://www.cnblogs.com/buro79xxd/
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 = [[MethodForSelectorsalloc]init];
NSArray *Arrays = [NSArrayarrayWithObjects:@"AAA",@"BBB", nil];
for (NSString *array in Arrays ){
SEL customSelector =NSSelectorFromString([NSStringstringWithFormat:@"abcWith%@:", array]);
mfs = [[MethodForSelectorsalloc] performSelector:customSelectorwithObject: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 = [numberintValue];
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 = [MethodForSelectorsalloc];
NSArray *Arrays = [NSArrayarrayWithObjects:@"AAA", @"BBB", nil];
for ( NSString *array in Arrays ){
SEL customSelector =NSSelectorFromString([NSStringstringWithFormat:@"abcWith%@:", array]);
mfs = [[MethodForSelectorsalloc] 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中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector withObject
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:(转)
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Java多线程设计wait、notify、notifyall、synchronized的使用机制
- TestComplete资源列表
- 字符串操作函数记录
- LoadRunner 常见问题解决
- shell的执行过程
- Objective-C中一种消息处理方法performSelector: withObject:
- Struts2配置及国际化配置
- Android有效解决加载大图片时内存溢出的问题
- Failed to load JavaHL Library. These are the errors that were encountered: n
- android通过ksoap2和axis调用web service
- 在android4.2中添加自己的intent事件并使用
- 带有验证的soap webservice
- firefox在linux中的安装
- 设计模式--策略模式--UML图