SEL就是对类中方法的包装或者相当于C语言中的函数指针,同一个类中不能有同名函数,即使参数类型不同也不行,因为SEL就是根据函数名而确定的
SEL定义:
/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SEL selTest = @selector(test);
SEL selTest1 = NSSelectorFromString(@"test");
[self performSelector:selTest withObject:nil];
[self performSelector:selTest1 withObject:nil];
NSString * selTestStr = NSStringFromSelector(selTest);
NSLog(@"%@", selTestStr);
SEL test1 = @selector(test1:);
SEL test11 = NSSelectorFromString(@"test1:");
[self performSelector:test1 withObject:@"params"];
[self performSelector:test11 withObject:@"params"];
SEL test2 = @selector(test:withStr:);
[self performSelector:test2 withObject:@"s1" withObject:@"s2"];
}
- (void) test{
NSLog(@"test-------");
}
- (void) test:(NSString *) str withStr:(NSString *) str2{
NSLog(@"test2--%@--%@", str, str2);
}
- (void) test1:(NSString *) str {
NSLog(@"test1--%@", str);
}
输出:
2015-11-02 13:16:42.856 02-runtime[2384:64080] test-------
2015-11-02 13:16:42.857 02-runtime[2384:64080] test-------
2015-11-02 13:16:42.859 02-runtime[2384:64080] test
2015-11-02 13:16:42.861 02-runtime[2384:64080] test1--params
2015-11-02 13:16:42.861 02-runtime[2384:64080] test1--params
2015-11-02 13:16:42.862 02-runtime[2384:64080] test2--s1--s2
好像最多只有两个参数的,没有performSelector:参数 withObject:参数 withObject:参数 withObject:参数 这样的函数