标签:
同函数指针的效果一样,方便消息传递
SEL sel = @selector(method);创建SEL
NSLog(@"The selector of action is %@",NSStringFromSelector(sel));打印SEL对应方法的名称
- (BOOL)respondsToSelector:(SEL)aSelector;检查类中是否含有aSelector所对应的方法
调用方法
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
performSelector方法最多包含两个参数,更多参数请使用字典或数组传递
范例代码
Person.h
-(void)message; -(void)message:(NSString *)name; -(void)message:(NSString *)name andWork:(NSString *)work;
main.m
Person *person=[[Person alloc]init]; SEL selMessage = @selector(message); NSLog(@"selMessage method is:%@",NSStringFromSelector(selMessage)); if([person respondsToSelector:selMessage]) { [person performSelector:selMessage]; } if([person respondsToSelector:@selector(message:)]){ [person performSelector:@selector(message:) withObject:@"Mary"]; } if([person respondsToSelector:@selector(message:andWork:)]) { [person performSelector:@selector(message:andWork:) withObject:@"Mary" withObject:@"teacher"]; }
标签:
原文地址:http://www.cnblogs.com/aY-Wonder/p/4561136.html