iOS笔记系列目录
一 概念
- SEL
办法名(编号)
- IMP
一个函数指针,保留了办法的地址
- @selector(办法名)
获取办法的编号,后果是SEL类型。他的行为根本能够等同于C语言中的函数指针
二 区别
C语言中,能够间接把函数名赋值给一个函数指针,而且函数指针间接保留了函数地址Objc中的类不能间接利用函数指针,只能应用@selector来获取,获取的是办法的编号
三 原理
办法以@selector作为索引,@selector的数据类型是SEL,对应每个办法的编号,当咱们寻找办法的时候应用的是这个办法编号。类中存在一个methodLists专门用来寄存办法实现IMP和SEL的映射。办法编号SEL通过Dispatch table表寻找到对应的IMP,IMP就是一个函数指针,而后执行这个办法。
struct objc_class { struct objc_class super_class; /*父类*/ const char *name; /*类名字*/ long version; /*版本信息*/ long info; /*类信息*/ long instance_size; /*实例大小*/ struct objc_ivar_list *ivars; /*实例参数链表*/ struct objc_method_list **methodLists; /*办法链表*/ struct objc_cache *cache; /*办法缓存*/ struct objc_protocol_list *protocols; /*协定链表*/};
typedef struct objc_method *Method;typedef struct objc_ method { SEL method_name; //办法名 char *method_types; //参数类型 IMP method_imp; //办法实现};
四 相干
class 返回对象的类;isKindOfClass 和 isMemberOfClass查看对象是否在指定的类继承体系中;respondsToSelector 查看对象是否相应指定的音讯;conformsToProtocol 查看对象是否实现了指定协定类的办法;methodForSelector 返回指定办法实现的地址。performSelector:withObject 执行SEL 所指代的办法。