OC 底层原理之对象的本质

1、语言转换流程

OC --> C/C++ --> 汇编语言 --> 机器语言

分析:
1、OC 代码底层实现是 C/C++ 代码
2、机器语言是计算机能识别的唯一语言
3、OC 的对象、类主要是基于 C/C++ 的结构体实现的

将源码文件转为 C/C++ 代码:
不指定系统架构:clang -rewrite-objc main.m -o main.cpp (将 main.m 的 OC 源代码文件转换为 main.cpp 的 C++ 代码文件)。
指定系统架构:xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp(xcrun -sdk iphoneos:指定是 iphoneos 系统,-arch arm64:arm64 架构)

2、NSObject 的底层实现

1、NSObject 源码

@interface NSObject  {
    Class isa;
}

2、实现
struct NSObject_IMPL {
    Class isa;
}

3、Class 对象
typedef struct objc_class *Class;

3、相关面试题

1、一个 NSObject 对象占用多少内存?

CoreFoundation 框架规定,当对象内存少于 16 个字节的时候,系统会分配 16 个字节给对象(可以通过 malloc_size 函数查看),但 NSObject 对象内部只使用了8个字节的空间(可以通过 class_getInstanceSize 函数查看,该 8 个字节空间用来是存储 isa 指针)。

2、以下 student 占用多少内存
1、源码
@interface Student: NSObject{
    @public
    int _no;
    int _age;
}
@end

2、实现
struct Student_IMPL {
    struct NSObject_IMPL;
    int _no;
    int _age;
}
struct NSObject_IMPL {
    Class isa;
}
typedef struct objc_class *Class;

3、使用
Student *stu = [[Student alloc] init];
stu->_no = 4;
stu->_age = 5;

4、分析
系统为 Student 对象分配了 16 字节内存。
实际占用 16 个字节,其中前 8 个字节用来存放 isa 指针,接下来 4 个字节用来存放 int 类型的 _no,再接下来的 4 个字节用来存放 int 类型的 _age。
3、以下 student 和 person 各占用多少内存
@interface Person: NSObject{
    int _no;
}
@end

@interface Student: Person{
    int _age;
}
@end

分析:
Person 对象:
分配了 16 个字节。
实际占用 12 个字节,其中前 8 个字节用来存放 isa 指针,接下来的 4 个字节用来存放 int 类型的 _no。
Student 对象:
分配了 16 个字节。
实际占用 16 个字节,其中前 8 个字节用来存放 isa 指针,接下来 4 个字节用来存放 Person 对象中的 int 类型的 _no,再接下来的 4 个字节用来存放 int 类型的 _age。
4、以下 student 占用多少内存
1、源码
@interface Student: NSObject{
    @public
    int _no;
    int _age;
    int _height;
}
@end

2、实现
struct Student_IMPL {
    struct NSObject_IMPL;
    int _no;
    int _age;
    int _height;
}
struct NSObject_IMPL {
    Class isa;
}
typedef struct objc_class *Class;

3、分析
> 系统为 Student 对象分配了 32 字节内存。
  由于 Student 实际上至少要 20 字节,根据操作系统内存对齐原则(16 字节的整数倍),故需要分配 32 字节。
> 实际占用 24 个字节,
  其中前 8 个字节用来存放 isa 指针,
  接下来 4 个字节用来存放 int 类型的 _no,
  再接下来的 4 个字节用来存放 int 类型的 _age,
  再接下来的 4 个字节用来存放 int 类型的 _height,
  根据结构体内存对齐原则,实际占用内存是结构体中最大成员的整数倍,即 NSObject_IMPL(8 字节) 整数倍为 24 字节。
5、以下 student 占用多少内存
1、源码
@interface Student: NSObject{
    int _no;
}
@property (nonatomic,assign) int height;
@end

2、实现
struct Student_IMPL {
    struct NSObject_IMPL;
    int _no;
    int _height;
}

3、结果
系统为 Student 对象分配了 16 字节内存。
实际占用 16 个字节,其中前 8 个字节用来存放 isa 指针,接下来 4 个字节用来存放 int 类型的 _no,再接下来的 4 个字节用来存放 int 类型的 _height。

4、分析
@property (nonatomic,assign) int height;
其实际上是由成员变量 _height 和 set/get 方法构成,所以其实际上呈现的如上面实现中的内容。
实例对象里面只存放 isa 指针和成员变量并不存放方法,原因如下:
类对象有且只有一个,但类对象的实例对象可以有很多个;
实例对象中存放成员变量用来保持每个实例对象的唯一性;
实例对象中不存放方法,是因为方法是存放在类对象的方法列表中,可以保证类的每个实例对象都可以使用该方法;

4、OC 对象分类

Objective-C 中的对象,主要可以分为 3 种 instance 对象(实例对象)、class 对象(类对象)、meta-class 对象(元类对象) 。

1、instance 对象

instance 对象就是通过类 alloc 出来的对象,每次调用 alloc 都会产生新的 instance 对象。instance 对象在内存中存储的信息包括 isa 指针和成员变量的值。

2、class 对象

每个类在内存中有且只有一个 class 对象。class 对象在内存中存储的信息主要包括 isa 指针、superclass 指针、类的属性信息(@property)、类的对象方法信息(instance method)、类的协议信息(protocol)、类的成员变量信息(ivar 的类型、名字等)等等。

3、meta-class 对象

类的类。每个类在内存中有且只有一个 meta-class 对象,meta-class 对象和 class 对象的内存结构是一样的,但是用途不一样,meta-class 对象的信息主要包括 isa 指针、superclass 指针、类的类方法信息(class method)。

5、isa 和 superclass

isa & superclass

小结:
1、isa 指向
instance 的 isa 指向 class;
class 的 isa 指向 meta-class;
meta-class 的 isa 指向基类的 meta-class。

2、superclass 指向
class 的 superclass 指向父类的 class,如果没有父类,superclass 指针为 nil;
meta-class 的 superclass 指向父类的 meta-class,基类的 meta-class 的 superclass 指向基类的 class。

3、方法调用
instance 方法调用:instance 通过 isa 找到 class,在 class 方法列表中查找方法。若方法不存在,class 通过 superclass 找到父类,在父类的方法列表中继续查找;
class 方法调用:class 通过 isa 找到 meta-class,在 meta-class 方法列表中查找方法。若方法不存在,meta-class 通过 superclass 找到父类,在父类的方法列表中继续查找。

补充

1、常用 LLDB 指令

打印
print、p:打印
po:打印对象

读取内存
memory read/数量格式字节数 内存地址
x/数量格式字节数 内存地址
例:x/3xw 0x10010

修改内存中的值
memory write 内存地址 数值
例:memory write 0x0000010 10

格式说明
x是16进制,f是浮点,d是10进制

字节大小说明
b:byte 1字节,h:half word 2字节
w:word 4字节,g:giant word 8字节

2、内存对齐原则

结构体的内存对齐原则
结构体的大小必须是其中最大成员大小的倍数

操作系统的内存对齐原则
操作系统对于堆空间的内存对齐原则为 16 字节的整数倍

你可能感兴趣的:(OC 底层原理之对象的本质)