OC - SEL类型的使用
2016-01-26 23:47
561 查看
一.SEL类型的第一个作用, 配合对象/类来检查对象/类中有没有实现某一个方法
判断p对象中有没有实现-号开头的setAge:方法
如果P对象实现了setAge:方法那么就会返回YES
如果P对象没有实现setAge:方法那么就会返回NO
A. respondsToSelector注意点:
1. 如果是通过一个对象来调用该方法那么会判断该对象有没有实现-号开头的方法
2. 如果是通过类来调用该方法, 那么会判断该类有没有实现+号开头的方法
二. SEL类型的第二个作用, 配合对象/类来调用某一个SEL方法
调用p对象中sel类型对应的方法
withObject: 需要传递的参数
A.注意:
如果通过performSelector调用有参数的方法, 那么参数必须是对象类型,
也就是说方法的形参必须接受的是一个对象, 因为withObject只能传递一个对象
B.注意:performSelector最多只能传递2个参数
三. 配合对象将SEL类型作为方法的形参
SEL sel = @selector(setAge:); Class *p = [Class new];
判断p对象中有没有实现-号开头的setAge:方法
如果P对象实现了setAge:方法那么就会返回YES
如果P对象没有实现setAge:方法那么就会返回NO
BOOL flag = [p respondsToSelector:sel]; NSLog(@"flag = %i", flag);
A. respondsToSelector注意点:
1. 如果是通过一个对象来调用该方法那么会判断该对象有没有实现-号开头的方法
2. 如果是通过类来调用该方法, 那么会判断该类有没有实现+号开头的方法
SEL sel = @selector(test); flag = [p respondsToSelector:sel]; NSLog(@"flag = %i", flag); flag = [Class respondsToSelector:sel]; NSLog(@"flag = %i", flag);
二. SEL类型的第二个作用, 配合对象/类来调用某一个SEL方法
SEL sel = @selector(test); Class *p = [Class new];
调用p对象中sel类型对应的方法
[p performSelector:sel]; SEL sel = @selector(signalWithNumber:);
withObject: 需要传递的参数
A.注意:
如果通过performSelector调用有参数的方法, 那么参数必须是对象类型,
也就是说方法的形参必须接受的是一个对象, 因为withObject只能传递一个对象
[p performSelector:sel withObject:@"XXXXX"]; SEL sel2 = @selector(setAge:); [p performSelector:sel withObject:@"XXXXX"]; NSLog(@"age = %i", p.age);
B.注意:performSelector最多只能传递2个参数
SEL sel = @selector(sendMessageWithNumber:andContent:); [p performSelector:sel withObject:@"XXXXX" withObject:@"XXXXX"];
三. 配合对象将SEL类型作为方法的形参
Class *c = [Class new]; SEL sel = @selector(test); ClassTwo *p = [ClassTwo new]; [p makeObject:c andSel:sel];
附上apple文档:(一切以以下为准) • Defines an opaque type that represents a method selector. Declaration typedef struct objc_selector *SEL; Discussion Method selectors are used to represent the name of a method at runtime. A method selector is a C string that has been registered (or “mapped“) with the Objective-C runtime. Selectors generated by the compiler are automatically mapped by the runtime when the class is loaded. You can add new selectors at runtime and retrieve existing selectors using the function sel_registerName. When using selectors, you must use the value returned from sel_registerName or the Objective-C compiler directive @selector(). You cannot simply cast a C string to SEL. Import Statement objc_method_description Defines an Objective-C method. Declaration struct objc_method_description { SEL name; char *types; }; Fields name The name of the method at runtime. types The types of the method arguments.
相关文章推荐
- 黑马程序员IOS-OC语言-类的本质、SEL
- OC语法<3.4> OC中特有的语法:SEL基本数据类型
- 关于CLASS , SEL, IMP的说明
- ios之SEL
- OC - 类的基本定义与方法的使用
- objective-c之sel----字符串转化成sel数据(变量)----sel数据(变量)转化成字符串输出
- OC学习:SEL类型数据和selector的用法
- iOS SEL(@selector)原理
- iOS Runtime详解之SEL,Class,id,IMP,_cmd,isa,method,Ivar
- Objective-C数据类型之id,SEL,BOOL,nil,NULL和NSNull
- iPhone App开发实战手册学习笔记(8)之Objective-C消息调用原理
- OC学习笔记(10)Object-C方法链表
- iOS SEL类型
- cannot assign to 'self' outside of a method in the init family
- Method Swizzle黑魔法,修改 ios 系统类库方法
- IOS 关于SEL的简单总结
- SEL数据类型的简单知识点——和方法有关
- Objective中SEL和@selecter精讲
- SEL的使用
- SEL