SEL即为selector的缩写。
一般以报错的方式来检测一个类中是否存在某个类方法或对象方法。
SEL的几种用法:
1. 配合对象或者类检查类或对象中是否有某个方法
//setAge为对象中的方法
//如果对象中没有setAge方法就会报错
SEL sel = @selector(setAge);
Person *p = [[Person alloc] init];
BOOL flag = [p respondsToSelector:sel];
//注: 如果是对象调用这个方法(respondsToSelector:)那么是检查对象中有没有这样(sel)一个对象方法, 如果是类那个就是判断类中是否有这个类方法
BOOL flag2 = [Person respondsToSelector:sel];
2. 调用类或对象的某个SEL方法
SEL sel = @selector(setAge);
//如果sel是对象方法
Person *p = [[Person alloc] init];
[p performSelector:sel];
//如果sel是类方法
[Person performSelector:sel];
//注: performSelector这个方法如果要带参数需要接withObject: ,并且最多只能带两个参数,且参数必须是对象
[p performSelector:self withObject:@"123"];
3.将SEL类型作为方法的形参
//将sel作为传入的参数使用
SEL sel = @selector(setAge);