performSelector: withObject: 详细教程
来源:互联网 发布:淘宝复制宝贝 编辑:程序博客网 时间:2024/06/07 20:55
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
- performSelector: withObject: 详细教程
- iOS performSelector: withObject: 详细教程
- performSelector:withObject:方法
- performSelector:withObject:afterDelay:
- performSelector: withObject传递参数用法
- 关于performSelector:withObject:withObject:]: message sent to deallocated错误
- Objective-C performSelector: withObject: 消息处理方法
- 延迟执行 [self performSelector: withObject: afterDelay: ];
- 在ARC项目中使用 performSelector: withObject:
- IOS学习之performSelector:withObject:afterDelay:
- iOS 取消延时执行 performSelector: withObject: afterDelay:
- [绍棠] performSelector:withObject:afterDelay: 延迟执行
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Objective-C中一种消息处理方法performSelector: withObject:
- Oracle的NULL代表的含义是不确定,那么不确定的东西也会有确定的数据类型吗
- is-A和has-A的区别
- FastReport 1.6打印39条码问题
- TextView属性大全
- 使用stringstream对象简化类型转换
- performSelector: withObject: 详细教程
- 爱是永久相思情
- 约瑟夫环问题
- static Name & runtime value
- java设计模式
- MySQL优化经验
- minimalcomps accordion
- ruby 插入中文失败乱码
- OutputDebugString小例子