Objective-C 方法交换实践(一) - 基础知识
一、Objective-C 中的基本类型
首先看下 Objective-C 的对象模型,每个 Objective-C 对象都是一个指向 Class 的指针。Class 的结构如下:
struct objc_class {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class _Nullable super_class OBJC2_UNAVAILABLE;
const char * _Nonnull name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE;
struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE;
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
这个结构已经有很多的说明了,下面简单的再描述下
1. 变量列表
变量 Ivar 也是一个结构体,每个 Class 中用变长结构体的方式存储了 Class 的变量列表。 IVar 的定义如下,包含 名称、类型、偏移、占用空间。
typedef struct objc_ivar *Ivar;
struct objc_ivar {
char * _Nullable ivar_name OBJC2_UNAVAILABLE;
char * _Nullable ivar_type OBJC2_UNAVAILABLE;
int ivar_offset OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
这个变长结构体定义如下:
struct objc_ivar_list {
int ivar_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
2. 方法列表
每个方法 Method 的定义如下,包含 SEL 指向对外的命名,char * 型 的方法类型, IMP 方法指针,指向具体的函数实现。
typedef struct objc_method *Method;
struct objc_method {
SEL _Nonnull method_name OBJC2_UNAVAILABLE;
char * _Nullable method_types OBJC2_UNAVAILABLE;
IMP _Nonnull method_imp OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
同样一个变长结构体来存储方法列表。Class 中的这个列表是个2级指针,所以可以向 Class 中动态的添加方法。
struct objc_method_list {
struct objc_method_list * _Nullable obsolete OBJC2_UNAVAILABLE;
int method_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_method method_list[1] OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
3. 缓存
同样一个变长结构体存储之前找到的 Method。
1)、mask:可以认为是当前能达到的最大index(从0开始的),所以缓存的size(total)是mask+1;
2)、occupied:被占用的槽位,因为缓存是以散列表的形式存在的,所以会有空槽,而occupied表示当前被占用的数目。
他是通过 要查找的 Method 的 SEL 地址和 mask 做一系列运算来确定 Method 的存储与查找位置。更详细的说明可以看参考4。其中提到的几点也在说下:
子类的 cache 会存储在父类中找到的方法;cache 的大小会动态增加,但是增加之前一定会先清空自己(变长结构体的特性)。
typedef struct objc_cache *Cache OBJC2_UNAVAILABLE;
#define CACHE_BUCKET_NAME(B) ((B)->method_name)
#define CACHE_BUCKET_IMP(B) ((B)->method_imp)
#define CACHE_BUCKET_VALID(B) (B)
#ifndef __LP64__
#define CACHE_HASH(sel, mask) (((uintptr_t)(sel)>>2) & (mask))
#else
#define CACHE_HASH(sel, mask) (((unsigned int)((uintptr_t)(sel)>>3)) & (mask))
#endif
struct objc_cache {
unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE;
unsigned int occupied OBJC2_UNAVAILABLE;
Method _Nullable buckets[1] OBJC2_UNAVAILABLE;
};
4. 协议
typedef struct objc_category *Category;
struct objc_category {
char * _Nonnull category_name OBJC2_UNAVAILABLE;
char * _Nonnull class_name OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE;
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
struct objc_protocol_list {
struct objc_protocol_list * _Nullable next;
long count;
__unsafe_unretained Protocol * _Nullable list[1];
};
5. isa 和 superClass
看一张经典的图:

isa 表明当前对象所属于的 Class 类型(Class 也是一个对象,Class 的类型叫 MetaClass)。
superClass 表明当前对象从哪个父类派生出来的,根类型(比如 NSObject、NSProxy)的 superClass 是 nil。
向对象发送消息时,会去方法列表里面查询,找不到会去父类的方法列表,再找不到会进入动态添加、消息转发、消息包装的过程。向 Class 发送消息时,会去 MetaClass 走同样的过程。
二、self 和 super
self 是类的隐藏的参数,指向当前调用方法的类
super 是一个"编译器指示符", 是一个标记,告诉编译器起始于当前类的父类方法列表中搜索方法的实现。
看一个例子
@A
- (void)show{
}
- (void)log {
NSLog(@"i am a");
}
- (void)print {
NSLog(@"i am %@",[self class]);
}
@end
@B: A
- (void)show
{
[self/super log];
[self/super print];
}
- (void)log {
NSLog(@"i am b");
}
- (void)print {
NSLog(@"i am %@",[self class]);
}
@end
@ C: B
- (void)log {
NSLog(@"i am c");
}
@end
在 B 的show 方法中分别改成 self 和 super,如下调用会输出什么?
C *c = [[C alloc] init];
[c show];
结果是 self 的时候 输出
i am c
i am C
super 的时候输出
i am a
i am C
用 self 调用方法,会编译成 objc_msgSend 方法,其定义如下:
void objc_msgSend(void /* id self, SEL op, ... */ )
第一个参数是消息接收者,也就是对象本身,第二个参数是调用的具体类方法的 selector。这里有个隐藏参数 _cmd,代表当前类方法的selector。
用super 调用方法,会编译成 objc_msgSendSuper 方法,其定义如下:
void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
其中 objc_super 的定义如下:
/// Specifies the superclass of an instance.
struct objc_super {
/// Specifies an instance of a class.
__unsafe_unretained _Nonnull id receiver;
/// Specifies the particular superclass of the instance to message.
#if !defined(__cplusplus) && !__OBJC2__
/* For compatibility with old objc-runtime.h header */
__unsafe_unretained _Nonnull Class class;
#else
__unsafe_unretained _Nonnull Class super_class;
#endif
/* super_class is the first class to search */
};
三、消息转发
当向一个类的实例发送方法时,会去 Class 结构的方法缓存列表 objc_cache 和 方法列表 objc_method_list 中查找有没有这个方法,如果没有的话,则会进入消息转发阶段。
消息转发主要分为两大阶段:
动态方法解析:看对象所属类是否能动态添加方法
转发阶段:既然第一步已经不会新增方法来响应,那系统就会请接受者看看有没有其他对象响应这个消息;如果没有,就把消息封装到 NSInvocation中,再做一次尝试。
参考:
1.http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
2.http://time-track.cn/variable-length-structure.html
3.https://tech.meituan.com/DiveIntoMethodCache.html
4.http://blog.csdn.net/datacloud/article/details/7275170
5.http://blog.csdn.net/wzzvictory/article/details/8487111
Objective-C 方法交换实践(一) - 基础知识的更多相关文章
- Python入门方法推荐,哪些基础知识必学?
很多想入门的小伙伴还不知道Python应该怎么学,哪些知识必学,今天我们就来盘点一下. 01.入门方法推荐 总体来讲,找一本靠谱的书,由浅入深,边看边练. 网上的学习教程有很多,多到不知道如何选择.所 ...
- 《Python编程:从入门到实践》基础知识部分学习笔记整理
简介 此笔记为<Python编程:从入门到实践>中前 11 章的基础知识部分的学习笔记,不包含后面的项目部分. 书籍评价 从系统学习 Python 的角度,不推荐此书,个人更推荐使用< ...
- Objective-C 方法交换实践(二) - 方法指针交换
一. 基本函数 根据 sel 得到 class 的实例方法 Method class_getInstanceMethod(Class cls, SEL name) 根据 sel 得到 class 的函 ...
- Objective-C 方法交换实践(三) - Aspects 源码解析
一.类与变量 AspectOptions typedef NS_OPTIONS(NSUInteger, AspectOptions) { AspectPositionAfter = 0, /// 原方 ...
- 学习nginx从入门到实践(四) 基础知识之nginx基本配置语法
nginx基本配置语法 1.http相关 展示每次请求的请求头: curl -v http://www.baidu.com 2.nginx日志类型 error.log. access.log log_ ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- 20151024_004_C#基础知识(C#中的访问修饰符,继承,new关键字,里氏转换,is 和 as,多态,序列化与反序列化)
1:C#中的访问修饰符 public: 公共成员,完全公开,没有访问限制. private: 私有的,只能在当前类的内部访问. protected: 受保护的,只能在当前类的内部以及该类的子类中访问. ...
- kubebuilder实战之三:基础知识速览
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 0.Python 爬虫之Scrapy入门实践指南(Scrapy基础知识)
目录 0.0.Scrapy基础 0.1.Scrapy 框架图 0.2.Scrapy主要包括了以下组件: 0.3.Scrapy简单示例如下: 0.4.Scrapy运行流程如下: 0.5.还有什么? 0. ...
随机推荐
- VirtualBox操作总结
1. VirtualBox安装 下载rpm,rpm -ivh 安装 2. 在图形界面打开virtual box virtualbox 3. vboxmanage打开虚拟机 vboxmanage sta ...
- Hadoop学习之路(二十五)MapReduce的API使用(二)
学生成绩---增强版 数据信息 computer,huangxiaoming,85,86,41,75,93,42,85 computer,xuzheng,54,52,86,91,42 computer ...
- indexzero/http-server-1-简介
ethereumjs-vm的例子有用到这个 看完ethereumjs-vm,再看他ethereumjs/browser-builds,然后最后看他http-server,然后就能了解ethereumj ...
- git用户限制ssh登录服务器
服务器额外的防范措施: 搭建git服务器后通常会建立一个git账户,其它人共用这个账户来克隆或推送数据到git仓库中,通常也只需要这个功能,但是如果不加限制,那么其它人可以通过这个git账户登录到主机 ...
- 一种基于openflow的虚拟化层软件flowvisor的API测试
注明:本文并不对openflow进行分析,本人也是略略知道这个概念,对flowvisor也只是对其API有所测试,更深的源码并未涉及,只是希望该文能对以后的flowvisor研究者提供些许帮助. 一: ...
- 可视化编程中----cell的自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ Joke ...
- org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping
org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping 原因:yml文件格式错误,此文件要求严格要求格式 如节 ...
- 【js】 ajax 与 axios 区别
ajax 与 axios区别 Ajax: Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发 ...
- source .bashrc 报错:virtualenvwrapper.sh: There was a problem running the initialization hooks.
在Ubuntu下安装完virtualenv.virtualenvwrapper,然后设置环境文件 .bashrc 接着 source .bashrc,产生错误信息 首先确认了 libpam-mount ...
- react-navigation的超级大坑
本文针对react-navigation^3.0.0版本,版本不对的话,请不要看本文,直接看官方英文文档 最近一直在学习RN,没找到什么好的视频,所以一直看文档,一路上来虽然遇到一些乱七八糟的bu ...