mirror of
https://github.com/NohamR/knowledge-kit.git
synced 2026-05-25 12:27:15 +00:00
2100 lines
71 KiB
Markdown
2100 lines
71 KiB
Markdown
# 类别(Category)、拓展(Extension)、load、initialize
|
||
|
||
> 很多人都知道类别、分类的用法,但是对于一些细节就不是很清楚了,本文主要梳理下这3个概念的细节
|
||
|
||
|
||
|
||
## 类别(Category)
|
||
|
||
### 文件特征
|
||
|
||
- 类别文件有2个,分别为 .h 和 .m
|
||
- 命名为: “类名+类别名.h”和“类名+类别名.m”
|
||
|
||
### 文件内容格式
|
||
|
||
.h 文件格式
|
||
|
||
```
|
||
#import "类名.h"
|
||
|
||
@interface 类名 (类别名)
|
||
// 在此处声明方法
|
||
@end
|
||
```
|
||
|
||
.m 文件格式
|
||
|
||
```
|
||
#import "类名+类别名.h"
|
||
|
||
@implementation 类名 (类别名)
|
||
// 在此处实现声明的方法
|
||
@end
|
||
```
|
||
|
||
### 类别的作用
|
||
可以把类的实现分开在几个不同的源文件里,所以好处是:
|
||
- 减少耽搁文件的代码行数
|
||
- 可以把不痛的功能组织到不同的 category 里
|
||
- 可以由多个开发者共同完成一个大的类,方便协作
|
||
- 拓展当前类,为类添加方法
|
||
- 声明私有方法
|
||
|
||
### 类别的局限性
|
||
|
||
- 无法向现有的类添加实例变量(编译器报“instance variables may not be placed in categories”)。Category 一般只为类提供方法的拓展,不提供属性的拓展。但是利用 Runtime 可以在 Category 中添加属性
|
||
|
||
- 方法名称冲突的情况下,如果 Category 中的方法与当前类的方法名称重名,Category 具有更高的优先级,类别中的方法将完全取代现有类中的方法(调用方法的时候不会去调用现有类里面的方法实现)。
|
||
|
||
- 当现有类具有多个 Category 的时候,如果每个 Category 都有同名的方法,那么在调用方法的时候肯定不会调用现有类的方法实现。系统根据编译顺序决定调用哪个 Category 下的方法实现。(可以在 Targets -> Build phases -> Compile Sources 下给多个 Category 更换顺序看看到底在执行哪个方法)
|
||
|
||
### Category 的使用和注意
|
||
|
||
1. Category 中的方法如果和现有类方法一致,工程中任何调用当前类的方法的时候都会去调用 Category 里面的方法(比如:UIViewCtroller、UITableView这些)的方法时要慎重。因为用Category重写类中的方法会对子类造成很大的影响。比如:用Category 重写了 UIViewCtroller 的方法 A,那么如果你在工程中用到的所有继承自 UIViewCtroller 的子类,去调用方法 A 时,执行的都是 Category 中重写的方法 A,如果不幸的是,你写的方法 A 有 Bug,那么会造成整个工程中调用该方法的所有 UIViewCtroller 子类的不正常。除非你在子类中重写了父类的方法 A,这样子类调用方法 A 时是调用的自己重写的方法 A,消除了父类 Category 中重写方法对自己的影响
|
||
|
||
2. Category拓展方法按照有没有重写当前类中的方法,分为未重写的拓展方法和重写拓展方法。且类引用自己的 Category 时,只能在 .m 文件中引用(.h 文件引用自己的类别会报错)。子类引用父类的 Category 在 .h 或 .m 都可以。如果类调用 Category 中重写的方法,不用引入 Category 头文件,系统会自动调用 Category 中的重写方法
|
||
|
||
3. Category 中如果重写了 A 类从父类继承来的某方法,不会影响与 A 同层级的 B 类
|
||
|
||
4. 子类会不会继承父类的 Category: Category 中重写的方法会对子类造成影响,但是子类不会继承非重写的方法(现有类中没有的方法)。但是在子类中引入父类 Category 的声明文件后,子类就会继承 Category 的非重写方法。继承的表现是:当子类的方法和父类 Category 中的方法名完全相同,那么子类里的方法会覆盖掉父类 Category,相当于子类重写了继承自父类的方法
|
||
|
||
5. Category 的作用是向下有效的。即只会影响到该类的所有子类。比如 A 类和 B 类是继承自 Super 类的2个子类,当给 A 类添加一个 Category sayHello 方法,仅有A 类的子类才可以使用 sayHello 方法
|
||
|
||
|
||
|
||
## Category 底层原理
|
||
|
||
### Category 是 category_t 结构体
|
||
|
||
来一个简单的 Person 类,为其添加一个 Category,增加一些属性和类方法、对象方法、遵循协议
|
||
|
||
```objectivec
|
||
// Person
|
||
#import <Foundation/Foundation.h>
|
||
NS_ASSUME_NONNULL_BEGIN
|
||
@interface Person : NSObject
|
||
@property (nonatomic, strong) NSString *name;
|
||
- (void)sayHi;
|
||
- (void)sleep;
|
||
@end
|
||
NS_ASSUME_NONNULL_END
|
||
|
||
// Person.m
|
||
#import "Person.h"
|
||
@implementation Person
|
||
- (void)sayHi {
|
||
NSLog(@"Hello world");
|
||
}
|
||
- (void)sleep{
|
||
NSLog(@"Time to slepp");
|
||
}
|
||
@end
|
||
|
||
// Person+Study.h
|
||
#import "Person.h"
|
||
NS_ASSUME_NONNULL_BEGIN
|
||
@interface Person (Study)<NSCopying>
|
||
@property (nonatomic, assign) NSInteger score;
|
||
- (void)study;
|
||
+ (void)sleep;
|
||
@end
|
||
NS_ASSUME_NONNULL_END
|
||
|
||
// Person+Study.m
|
||
#import "Person+Study.h"
|
||
@implementation Person (Study)
|
||
- (void)study {
|
||
|
||
}
|
||
+ (void)sleep {
|
||
NSLog(@"Time to sleep");
|
||
}
|
||
- (void)setScore:(NSInteger)score {
|
||
|
||
}
|
||
- (NSInteger)score {
|
||
return 100;
|
||
}
|
||
- (id)copyWithZone:(NSZone *)zone{
|
||
return self;
|
||
}
|
||
@end
|
||
```
|
||
|
||
clang 转为 c++ 代码,具体指令为 `xcrun --sdk iphoneos clang -arch arm64 -rewrite-objc Person+Study.m`
|
||
|
||
查看 `Person+Study.cpp` 文件,可以看到 Category 本质是一个结构体
|
||
|
||
```c++
|
||
struct _class_t {
|
||
struct _class_t *isa;
|
||
struct _class_t *superclass;
|
||
void *cache;
|
||
void *vtable;
|
||
struct _class_ro_t *ro;
|
||
};
|
||
|
||
struct _category_t {
|
||
const char *name;
|
||
struct _class_t *cls;
|
||
const struct _method_list_t *instance_methods;
|
||
const struct _method_list_t *class_methods;
|
||
const struct _protocol_list_t *protocols;
|
||
const struct _prop_list_t *properties;
|
||
};
|
||
extern "C" __declspec(dllimport) struct objc_cache _objc_empty_cache;
|
||
#pragma warning(disable:4273)
|
||
|
||
static struct /*_method_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _objc_method)
|
||
unsigned int method_count;
|
||
struct _objc_method method_list[4];
|
||
} _OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_objc_method),
|
||
4,
|
||
{{(struct objc_selector *)"study", "v16@0:8", (void *)_I_Person_Study_study},
|
||
{(struct objc_selector *)"setScore:", "v24@0:8q16", (void *)_I_Person_Study_setScore_},
|
||
{(struct objc_selector *)"score", "q16@0:8", (void *)_I_Person_Study_score},
|
||
{(struct objc_selector *)"copyWithZone:", "@24@0:8^{_NSZone=}16", (void *)_I_Person_Study_copyWithZone_}}
|
||
};
|
||
|
||
static struct /*_method_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _objc_method)
|
||
unsigned int method_count;
|
||
struct _objc_method method_list[1];
|
||
} _OBJC_$_CATEGORY_CLASS_METHODS_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_objc_method),
|
||
1,
|
||
{{(struct objc_selector *)"sleep", "v16@0:8", (void *)_C_Person_Study_sleep}}
|
||
};
|
||
|
||
static const char *_OBJC_PROTOCOL_METHOD_TYPES_NSCopying [] __attribute__ ((used, section ("__DATA,__objc_const"))) =
|
||
{
|
||
"@24@0:8^{_NSZone=}16"
|
||
};
|
||
|
||
static struct /*_method_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _objc_method)
|
||
unsigned int method_count;
|
||
struct _objc_method method_list[1];
|
||
} _OBJC_PROTOCOL_INSTANCE_METHODS_NSCopying __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_objc_method),
|
||
1,
|
||
{{(struct objc_selector *)"copyWithZone:", "@24@0:8^{_NSZone=}16", 0}}
|
||
};
|
||
|
||
struct _protocol_t _OBJC_PROTOCOL_NSCopying __attribute__ ((used)) = {
|
||
0,
|
||
"NSCopying",
|
||
0,
|
||
(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_NSCopying,
|
||
0,
|
||
0,
|
||
0,
|
||
0,
|
||
sizeof(_protocol_t),
|
||
0,
|
||
(const char **)&_OBJC_PROTOCOL_METHOD_TYPES_NSCopying
|
||
};
|
||
struct _protocol_t *_OBJC_LABEL_PROTOCOL_$_NSCopying = &_OBJC_PROTOCOL_NSCopying;
|
||
|
||
static struct /*_protocol_list_t*/ {
|
||
long protocol_count; // Note, this is 32/64 bit
|
||
struct _protocol_t *super_protocols[1];
|
||
} _OBJC_CATEGORY_PROTOCOLS_$_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
1,
|
||
&_OBJC_PROTOCOL_NSCopying
|
||
};
|
||
|
||
static struct /*_prop_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _prop_t)
|
||
unsigned int count_of_properties;
|
||
struct _prop_t prop_list[1];
|
||
} _OBJC_$_PROP_LIST_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_prop_t),
|
||
1,
|
||
{{"score","Tq,N"}}
|
||
};
|
||
|
||
extern "C" __declspec(dllimport) struct _class_t OBJC_CLASS_$_Person;
|
||
|
||
static struct _category_t _OBJC_$_CATEGORY_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) =
|
||
{
|
||
"Person",
|
||
0, // &OBJC_CLASS_$_Person,
|
||
(const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_Study,
|
||
(const struct _method_list_t *)&_OBJC_$_CATEGORY_CLASS_METHODS_Person_$_Study,
|
||
(const struct _protocol_list_t *)&_OBJC_CATEGORY_PROTOCOLS_$_Person_$_Study,
|
||
(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_Person_$_Study,
|
||
};
|
||
static void OBJC_CATEGORY_SETUP_$_Person_$_Study(void ) {
|
||
_OBJC_$_CATEGORY_Person_$_Study.cls = &OBJC_CLASS_$_Person;
|
||
}
|
||
```
|
||
|
||
可以看到 `Person+Study` 的 Category 底层赋值代码如下,就是结构体对象的初始化(参考上面的结构体各个成员变量)
|
||
|
||
```c++
|
||
static struct _category_t _OBJC_$_CATEGORY_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) =
|
||
{
|
||
"Person",
|
||
0, // &OBJC_CLASS_$_Person,
|
||
(const struct _method_list_t *)&_OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_Study,
|
||
(const struct _method_list_t *)&_OBJC_$_CATEGORY_CLASS_METHODS_Person_$_Study,
|
||
(const struct _protocol_list_t *)&_OBJC_CATEGORY_PROTOCOLS_$_Person_$_Study,
|
||
(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_Person_$_Study,
|
||
};
|
||
```
|
||
|
||
`_OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_Study` 结构体存放的是对象方法信息,如下
|
||
|
||
```c++
|
||
static struct /*_method_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _objc_method)
|
||
unsigned int method_count;
|
||
struct _objc_method method_list[4];
|
||
} _OBJC_$_CATEGORY_INSTANCE_METHODS_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_objc_method),
|
||
4,
|
||
{{(struct objc_selector *)"study", "v16@0:8", (void *)_I_Person_Study_study},
|
||
{(struct objc_selector *)"setScore:", "v24@0:8q16", (void *)_I_Person_Study_setScore_},
|
||
{(struct objc_selector *)"score", "q16@0:8", (void *)_I_Person_Study_score},
|
||
{(struct objc_selector *)"copyWithZone:", "@24@0:8^{_NSZone=}16", (void *)_I_Person_Study_copyWithZone_}}
|
||
};
|
||
```
|
||
|
||
`_OBJC_$_CATEGORY_CLASS_METHODS_Person_$_Study` 结构体存放的是类方法信息,如下
|
||
|
||
```c++
|
||
static struct /*_method_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _objc_method)
|
||
unsigned int method_count;
|
||
struct _objc_method method_list[1];
|
||
} _OBJC_$_CATEGORY_CLASS_METHODS_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_objc_method),
|
||
1,
|
||
{{(struct objc_selector *)"sleep", "v16@0:8", (void *)_C_Person_Study_sleep}}
|
||
};
|
||
```
|
||
|
||
`_OBJC_CATEGORY_PROTOCOLS_$_Person_$_Study` 结构体存放的是遵循的协议信息,如下
|
||
|
||
```c++
|
||
static struct /*_protocol_list_t*/ {
|
||
long protocol_count; // Note, this is 32/64 bit
|
||
struct _protocol_t *super_protocols[1];
|
||
} _OBJC_CATEGORY_PROTOCOLS_$_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
1,
|
||
&_OBJC_PROTOCOL_NSCopying
|
||
};
|
||
```
|
||
|
||
`_OBJC_$_PROP_LIST_Person_$_Study` 存放的是 Category 中的属性信息,如下
|
||
|
||
```c++
|
||
static struct /*_prop_list_t*/ {
|
||
unsigned int entsize; // sizeof(struct _prop_t)
|
||
unsigned int count_of_properties;
|
||
struct _prop_t prop_list[1];
|
||
} _OBJC_$_PROP_LIST_Person_$_Study __attribute__ ((used, section ("__DATA,__objc_const"))) = {
|
||
sizeof(_prop_t),
|
||
1,
|
||
1,
|
||
{{"score","Tq,N"}}
|
||
};
|
||
```
|
||
|
||
查看 [Objc 4 源代码](http://opensource.apple.com/tarballs/objc4/),Category 定义如下
|
||
|
||
```c++
|
||
struct category_t {
|
||
const char *name;
|
||
classref_t cls;
|
||
WrappedPtr<method_list_t, method_list_t::Ptrauth> instanceMethods;
|
||
WrappedPtr<method_list_t, method_list_t::Ptrauth> classMethods;
|
||
struct protocol_list_t *protocols;
|
||
struct property_list_t *instanceProperties;
|
||
// Fields below this point are not always present on disk.
|
||
struct property_list_t *_classProperties;
|
||
|
||
method_list_t *methodsForMeta(bool isMeta) const {
|
||
if (isMeta) return classMethods;
|
||
else return instanceMethods;
|
||
}
|
||
|
||
property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi) const;
|
||
|
||
protocol_list_t *protocolsForMeta(bool isMeta) const {
|
||
if (isMeta) return nullptr;
|
||
else return protocols;
|
||
}
|
||
};
|
||
```
|
||
|
||
|
||
|
||
结论:
|
||
|
||
- 为某个类添加的分类,分类中可能有属性、对象方法、类方法、遵循的协议、协议方法,本质都是存储到 `category_t` 结构体里面。
|
||
- 当有多个分类的时候,是通过分类数组来承载的
|
||
|
||
|
||
|
||
### category 中定义的方法,存储在哪?
|
||
|
||
抛个问题:当对象调用方法的时候,不管这个方法是类自身的方法,还是通过分类添加的方法,本质都是通过 isa 指针去寻找方法实现,(如果是对象方法,则通过 instance 的 isa 去找到类对象,最后找到对象方法的实现去调用;如果是类对象方法,则通过 class 的 isa 找到元类对象,最后找到类方法的实现进行调用),那给 Category 添加的方法,是「**如何“塞到”类对象或者元类对象的方法列表中去的**」?
|
||
|
||
带着问题查看 [objc4 的源代码](http://opensource.apple.com/tarballs/objc4/) `objc-os.mm` 文件中的 `_objc_init` 方法
|
||
|
||
```c++
|
||
/***********************************************************************
|
||
* _objc_init
|
||
* Bootstrap initialization. Registers our image notifier with dyld.
|
||
* Called by libSystem BEFORE library initialization time
|
||
**********************************************************************/
|
||
void _objc_init(void) {
|
||
static bool initialized = false;
|
||
if (initialized) return;
|
||
initialized = true;
|
||
// fixme defer initialization until an objc-using image is found?
|
||
environ_init();
|
||
tls_init();
|
||
static_init();
|
||
lock_init();
|
||
exception_init();
|
||
_dyld_objc_notify_register(&map_images, load_images, unmap_image);
|
||
}
|
||
```
|
||
|
||
`_objc_init` 内部会调用 `map_images` 方法,其内部如下
|
||
|
||
```c++
|
||
/***********************************************************************
|
||
* map_images
|
||
* Process the given images which are being mapped in by dyld.
|
||
* Calls ABI-agnostic code after taking ABI-specific locks.
|
||
*
|
||
* Locking: write-locks runtimeLock
|
||
**********************************************************************/
|
||
void map_images(unsigned count, const char * const paths[],
|
||
const struct mach_header * const mhdrs[]) {
|
||
rwlock_writer_t lock(runtimeLock);
|
||
return map_images_nolock(count, paths, mhdrs);
|
||
}
|
||
```
|
||
|
||
`map_images` 内部会调用 `map_images_nolock`
|
||
|
||
```c++
|
||
void map_images_nolock(unsigned mhCount, const char * const mhPaths[],
|
||
const struct mach_header * const mhdrs[]) {
|
||
static bool firstTime = YES;
|
||
header_info *hList[mhCount];
|
||
uint32_t hCount;
|
||
size_t selrefCount = 0;
|
||
|
||
// Perform first-time initialization if necessary.
|
||
// This function is called before ordinary library initializers.
|
||
// fixme defer initialization until an objc-using image is found?
|
||
if (firstTime) {
|
||
preopt_init();
|
||
}
|
||
|
||
if (PrintImages) {
|
||
_objc_inform("IMAGES: processing %u newly-mapped images...\n", mhCount);
|
||
}
|
||
|
||
// Find all images with Objective-C metadata.
|
||
hCount = 0;
|
||
|
||
// Count classes. Size various table based on the total.
|
||
int totalClasses = 0;
|
||
int unoptimizedTotalClasses = 0;
|
||
{
|
||
uint32_t i = mhCount;
|
||
while (i--) {
|
||
const headerType *mhdr = (const headerType *)mhdrs[i];
|
||
|
||
auto hi = addHeader(mhdr, mhPaths[i], totalClasses, unoptimizedTotalClasses);
|
||
if (!hi) {
|
||
// no objc data in this entry
|
||
continue;
|
||
}
|
||
|
||
if (mhdr->filetype == MH_EXECUTE) {
|
||
// Size some data structures based on main executable's size
|
||
#if __OBJC2__
|
||
size_t count;
|
||
_getObjc2SelectorRefs(hi, &count);
|
||
selrefCount += count;
|
||
_getObjc2MessageRefs(hi, &count);
|
||
selrefCount += count;
|
||
#else
|
||
_getObjcSelectorRefs(hi, &selrefCount);
|
||
#endif
|
||
|
||
#if SUPPORT_GC_COMPAT
|
||
// Halt if this is a GC app.
|
||
if (shouldRejectGCApp(hi)) {
|
||
_objc_fatal_with_reason
|
||
(OBJC_EXIT_REASON_GC_NOT_SUPPORTED,
|
||
OS_REASON_FLAG_CONSISTENT_FAILURE,
|
||
"Objective-C garbage collection "
|
||
"is no longer supported.");
|
||
}
|
||
#endif
|
||
}
|
||
|
||
hList[hCount++] = hi;
|
||
|
||
if (PrintImages) {
|
||
_objc_inform("IMAGES: loading image for %s%s%s%s%s\n",
|
||
hi->fname(),
|
||
mhdr->filetype == MH_BUNDLE ? " (bundle)" : "",
|
||
hi->info()->isReplacement() ? " (replacement)" : "",
|
||
hi->info()->hasCategoryClassProperties() ? " (has class properties)" : "",
|
||
hi->info()->optimizedByDyld()?" (preoptimized)":"");
|
||
}
|
||
}
|
||
}
|
||
|
||
// Perform one-time runtime initialization that must be deferred until
|
||
// the executable itself is found. This needs to be done before
|
||
// further initialization.
|
||
// (The executable may not be present in this infoList if the
|
||
// executable does not contain Objective-C code but Objective-C
|
||
// is dynamically loaded later.
|
||
if (firstTime) {
|
||
sel_init(selrefCount);
|
||
arr_init();
|
||
|
||
#if SUPPORT_GC_COMPAT
|
||
// Reject any GC images linked to the main executable.
|
||
// We already rejected the app itself above.
|
||
// Images loaded after launch will be rejected by dyld.
|
||
|
||
for (uint32_t i = 0; i < hCount; i++) {
|
||
auto hi = hList[i];
|
||
auto mh = hi->mhdr();
|
||
if (mh->filetype != MH_EXECUTE && shouldRejectGCImage(mh)) {
|
||
_objc_fatal_with_reason
|
||
(OBJC_EXIT_REASON_GC_NOT_SUPPORTED,
|
||
OS_REASON_FLAG_CONSISTENT_FAILURE,
|
||
"%s requires Objective-C garbage collection "
|
||
"which is no longer supported.", hi->fname());
|
||
}
|
||
}
|
||
#endif
|
||
|
||
#if TARGET_OS_OSX
|
||
// Disable +initialize fork safety if the app is too old (< 10.13).
|
||
// Disable +initialize fork safety if the app has a
|
||
// __DATA,__objc_fork_ok section.
|
||
|
||
if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_13) {
|
||
DisableInitializeForkSafety = true;
|
||
if (PrintInitializing) {
|
||
_objc_inform("INITIALIZE: disabling +initialize fork "
|
||
"safety enforcement because the app is "
|
||
"too old (SDK version " SDK_FORMAT ")",
|
||
FORMAT_SDK(dyld_get_program_sdk_version()));
|
||
}
|
||
}
|
||
|
||
for (uint32_t i = 0; i < hCount; i++) {
|
||
auto hi = hList[i];
|
||
auto mh = hi->mhdr();
|
||
if (mh->filetype != MH_EXECUTE) continue;
|
||
unsigned long size;
|
||
if (getsectiondata(hi->mhdr(), "__DATA", "__objc_fork_ok", &size)) {
|
||
DisableInitializeForkSafety = true;
|
||
if (PrintInitializing) {
|
||
_objc_inform("INITIALIZE: disabling +initialize fork "
|
||
"safety enforcement because the app has "
|
||
"a __DATA,__objc_fork_ok section");
|
||
}
|
||
}
|
||
break; // assume only one MH_EXECUTE image
|
||
}
|
||
#endif
|
||
}
|
||
|
||
if (hCount > 0) {
|
||
_read_images(hList, hCount, totalClasses, unoptimizedTotalClasses);
|
||
}
|
||
firstTime = NO;
|
||
}
|
||
```
|
||
|
||
`map_images_nolock` 会调用 `_read_images` 方法,如下:
|
||
|
||
```c++
|
||
void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses)
|
||
{
|
||
header_info *hi;
|
||
uint32_t hIndex;
|
||
size_t count;
|
||
size_t i;
|
||
Class *resolvedFutureClasses = nil;
|
||
size_t resolvedFutureClassCount = 0;
|
||
static bool doneOnce;
|
||
TimeLogger ts(PrintImageTimes);
|
||
|
||
runtimeLock.assertWriting();
|
||
|
||
#define EACH_HEADER \
|
||
hIndex = 0; \
|
||
hIndex < hCount && (hi = hList[hIndex]); \
|
||
hIndex++
|
||
|
||
if (!doneOnce) {
|
||
doneOnce = YES;
|
||
|
||
#if SUPPORT_NONPOINTER_ISA
|
||
// Disable non-pointer isa under some conditions.
|
||
|
||
# if SUPPORT_INDEXED_ISA
|
||
// Disable nonpointer isa if any image contains old Swift code
|
||
for (EACH_HEADER) {
|
||
if (hi->info()->containsSwift() &&
|
||
hi->info()->swiftVersion() < objc_image_info::SwiftVersion3)
|
||
{
|
||
DisableNonpointerIsa = true;
|
||
if (PrintRawIsa) {
|
||
_objc_inform("RAW ISA: disabling non-pointer isa because "
|
||
"the app or a framework contains Swift code "
|
||
"older than Swift 3.0");
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
# endif
|
||
|
||
# if TARGET_OS_OSX
|
||
// Disable non-pointer isa if the app is too old
|
||
// (linked before OS X 10.11)
|
||
if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_11) {
|
||
DisableNonpointerIsa = true;
|
||
if (PrintRawIsa) {
|
||
_objc_inform("RAW ISA: disabling non-pointer isa because "
|
||
"the app is too old (SDK version " SDK_FORMAT ")",
|
||
FORMAT_SDK(dyld_get_program_sdk_version()));
|
||
}
|
||
}
|
||
|
||
// Disable non-pointer isa if the app has a __DATA,__objc_rawisa section
|
||
// New apps that load old extensions may need this.
|
||
for (EACH_HEADER) {
|
||
if (hi->mhdr()->filetype != MH_EXECUTE) continue;
|
||
unsigned long size;
|
||
if (getsectiondata(hi->mhdr(), "__DATA", "__objc_rawisa", &size)) {
|
||
DisableNonpointerIsa = true;
|
||
if (PrintRawIsa) {
|
||
_objc_inform("RAW ISA: disabling non-pointer isa because "
|
||
"the app has a __DATA,__objc_rawisa section");
|
||
}
|
||
}
|
||
break; // assume only one MH_EXECUTE image
|
||
}
|
||
# endif
|
||
|
||
#endif
|
||
|
||
if (DisableTaggedPointers) {
|
||
disableTaggedPointers();
|
||
}
|
||
|
||
if (PrintConnecting) {
|
||
_objc_inform("CLASS: found %d classes during launch", totalClasses);
|
||
}
|
||
|
||
// namedClasses
|
||
// Preoptimized classes don't go in this table.
|
||
// 4/3 is NXMapTable's load factor
|
||
int namedClassesSize =
|
||
(isPreoptimized() ? unoptimizedTotalClasses : totalClasses) * 4 / 3;
|
||
gdb_objc_realized_classes =
|
||
NXCreateMapTable(NXStrValueMapPrototype, namedClassesSize);
|
||
|
||
ts.log("IMAGE TIMES: first time tasks");
|
||
}
|
||
|
||
|
||
// Discover classes. Fix up unresolved future classes. Mark bundle classes.
|
||
for (EACH_HEADER) {
|
||
if (! mustReadClasses(hi)) {
|
||
// Image is sufficiently optimized that we need not call readClass()
|
||
continue;
|
||
}
|
||
|
||
bool headerIsBundle = hi->isBundle();
|
||
bool headerIsPreoptimized = hi->isPreoptimized();
|
||
|
||
classref_t *classlist = _getObjc2ClassList(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
Class cls = (Class)classlist[i];
|
||
Class newCls = readClass(cls, headerIsBundle, headerIsPreoptimized);
|
||
|
||
if (newCls != cls && newCls) {
|
||
// Class was moved but not deleted. Currently this occurs
|
||
// only when the new class resolved a future class.
|
||
// Non-lazily realize the class below.
|
||
resolvedFutureClasses = (Class *)
|
||
realloc(resolvedFutureClasses,
|
||
(resolvedFutureClassCount+1) * sizeof(Class));
|
||
resolvedFutureClasses[resolvedFutureClassCount++] = newCls;
|
||
}
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: discover classes");
|
||
|
||
// Fix up remapped classes
|
||
// Class list and nonlazy class list remain unremapped.
|
||
// Class refs and super refs are remapped for message dispatching.
|
||
|
||
if (!noClassesRemapped()) {
|
||
for (EACH_HEADER) {
|
||
Class *classrefs = _getObjc2ClassRefs(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
remapClassRef(&classrefs[i]);
|
||
}
|
||
// fixme why doesn't test future1 catch the absence of this?
|
||
classrefs = _getObjc2SuperRefs(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
remapClassRef(&classrefs[i]);
|
||
}
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: remap classes");
|
||
|
||
// Fix up @selector references
|
||
static size_t UnfixedSelectors;
|
||
sel_lock();
|
||
for (EACH_HEADER) {
|
||
if (hi->isPreoptimized()) continue;
|
||
|
||
bool isBundle = hi->isBundle();
|
||
SEL *sels = _getObjc2SelectorRefs(hi, &count);
|
||
UnfixedSelectors += count;
|
||
for (i = 0; i < count; i++) {
|
||
const char *name = sel_cname(sels[i]);
|
||
sels[i] = sel_registerNameNoLock(name, isBundle);
|
||
}
|
||
}
|
||
sel_unlock();
|
||
|
||
ts.log("IMAGE TIMES: fix up selector references");
|
||
|
||
#if SUPPORT_FIXUP
|
||
// Fix up old objc_msgSend_fixup call sites
|
||
for (EACH_HEADER) {
|
||
message_ref_t *refs = _getObjc2MessageRefs(hi, &count);
|
||
if (count == 0) continue;
|
||
|
||
if (PrintVtables) {
|
||
_objc_inform("VTABLES: repairing %zu unsupported vtable dispatch "
|
||
"call sites in %s", count, hi->fname());
|
||
}
|
||
for (i = 0; i < count; i++) {
|
||
fixupMessageRef(refs+i);
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: fix up objc_msgSend_fixup");
|
||
#endif
|
||
|
||
// Discover protocols. Fix up protocol refs.
|
||
for (EACH_HEADER) {
|
||
extern objc_class OBJC_CLASS_$_Protocol;
|
||
Class cls = (Class)&OBJC_CLASS_$_Protocol;
|
||
assert(cls);
|
||
NXMapTable *protocol_map = protocols();
|
||
bool isPreoptimized = hi->isPreoptimized();
|
||
bool isBundle = hi->isBundle();
|
||
|
||
protocol_t **protolist = _getObjc2ProtocolList(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
readProtocol(protolist[i], cls, protocol_map,
|
||
isPreoptimized, isBundle);
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: discover protocols");
|
||
|
||
// Fix up @protocol references
|
||
// Preoptimized images may have the right
|
||
// answer already but we don't know for sure.
|
||
for (EACH_HEADER) {
|
||
protocol_t **protolist = _getObjc2ProtocolRefs(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
remapProtocolRef(&protolist[i]);
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: fix up @protocol references");
|
||
|
||
// Realize non-lazy classes (for +load methods and static instances)
|
||
for (EACH_HEADER) {
|
||
classref_t *classlist =
|
||
_getObjc2NonlazyClassList(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
Class cls = remapClass(classlist[i]);
|
||
if (!cls) continue;
|
||
|
||
// hack for class __ARCLite__, which didn't get this above
|
||
#if TARGET_OS_SIMULATOR
|
||
if (cls->cache._buckets == (void*)&_objc_empty_cache &&
|
||
(cls->cache._mask || cls->cache._occupied))
|
||
{
|
||
cls->cache._mask = 0;
|
||
cls->cache._occupied = 0;
|
||
}
|
||
if (cls->ISA()->cache._buckets == (void*)&_objc_empty_cache &&
|
||
(cls->ISA()->cache._mask || cls->ISA()->cache._occupied))
|
||
{
|
||
cls->ISA()->cache._mask = 0;
|
||
cls->ISA()->cache._occupied = 0;
|
||
}
|
||
#endif
|
||
|
||
realizeClass(cls);
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: realize non-lazy classes");
|
||
|
||
// Realize newly-resolved future classes, in case CF manipulates them
|
||
if (resolvedFutureClasses) {
|
||
for (i = 0; i < resolvedFutureClassCount; i++) {
|
||
realizeClass(resolvedFutureClasses[i]);
|
||
resolvedFutureClasses[i]->setInstancesRequireRawIsa(false/*inherited*/);
|
||
}
|
||
free(resolvedFutureClasses);
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: realize future classes");
|
||
|
||
// Discover categories.
|
||
for (EACH_HEADER) {
|
||
category_t **catlist =
|
||
_getObjc2CategoryList(hi, &count);
|
||
bool hasClassProperties = hi->info()->hasCategoryClassProperties();
|
||
|
||
for (i = 0; i < count; i++) {
|
||
category_t *cat = catlist[i];
|
||
Class cls = remapClass(cat->cls);
|
||
|
||
if (!cls) {
|
||
// Category's target class is missing (probably weak-linked).
|
||
// Disavow any knowledge of this category.
|
||
catlist[i] = nil;
|
||
if (PrintConnecting) {
|
||
_objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with "
|
||
"missing weak-linked target class",
|
||
cat->name, cat);
|
||
}
|
||
continue;
|
||
}
|
||
|
||
// Process this category.
|
||
// First, register the category with its target class.
|
||
// Then, rebuild the class's method lists (etc) if
|
||
// the class is realized.
|
||
bool classExists = NO;
|
||
if (cat->instanceMethods || cat->protocols
|
||
|| cat->instanceProperties)
|
||
{
|
||
addUnattachedCategoryForClass(cat, cls, hi);
|
||
if (cls->isRealized()) {
|
||
remethodizeClass(cls);
|
||
classExists = YES;
|
||
}
|
||
if (PrintConnecting) {
|
||
_objc_inform("CLASS: found category -%s(%s) %s",
|
||
cls->nameForLogging(), cat->name,
|
||
classExists ? "on existing class" : "");
|
||
}
|
||
}
|
||
|
||
if (cat->classMethods || cat->protocols
|
||
|| (hasClassProperties && cat->_classProperties))
|
||
{
|
||
addUnattachedCategoryForClass(cat, cls->ISA(), hi);
|
||
if (cls->ISA()->isRealized()) {
|
||
remethodizeClass(cls->ISA());
|
||
}
|
||
if (PrintConnecting) {
|
||
_objc_inform("CLASS: found category +%s(%s)",
|
||
cls->nameForLogging(), cat->name);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
ts.log("IMAGE TIMES: discover categories");
|
||
|
||
// Category discovery MUST BE LAST to avoid potential races
|
||
// when other threads call the new category code before
|
||
// this thread finishes its fixups.
|
||
|
||
// +load handled by prepare_load_methods()
|
||
|
||
if (DebugNonFragileIvars) {
|
||
realizeAllClasses();
|
||
}
|
||
|
||
|
||
// Print preoptimization statistics
|
||
if (PrintPreopt) {
|
||
static unsigned int PreoptTotalMethodLists;
|
||
static unsigned int PreoptOptimizedMethodLists;
|
||
static unsigned int PreoptTotalClasses;
|
||
static unsigned int PreoptOptimizedClasses;
|
||
|
||
for (EACH_HEADER) {
|
||
if (hi->isPreoptimized()) {
|
||
_objc_inform("PREOPTIMIZATION: honoring preoptimized selectors "
|
||
"in %s", hi->fname());
|
||
}
|
||
else if (hi->info()->optimizedByDyld()) {
|
||
_objc_inform("PREOPTIMIZATION: IGNORING preoptimized selectors "
|
||
"in %s", hi->fname());
|
||
}
|
||
|
||
classref_t *classlist = _getObjc2ClassList(hi, &count);
|
||
for (i = 0; i < count; i++) {
|
||
Class cls = remapClass(classlist[i]);
|
||
if (!cls) continue;
|
||
|
||
PreoptTotalClasses++;
|
||
if (hi->isPreoptimized()) {
|
||
PreoptOptimizedClasses++;
|
||
}
|
||
|
||
const method_list_t *mlist;
|
||
if ((mlist = ((class_ro_t *)cls->data())->baseMethods())) {
|
||
PreoptTotalMethodLists++;
|
||
if (mlist->isFixedUp()) {
|
||
PreoptOptimizedMethodLists++;
|
||
}
|
||
}
|
||
if ((mlist=((class_ro_t *)cls->ISA()->data())->baseMethods())) {
|
||
PreoptTotalMethodLists++;
|
||
if (mlist->isFixedUp()) {
|
||
PreoptOptimizedMethodLists++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
_objc_inform("PREOPTIMIZATION: %zu selector references not "
|
||
"pre-optimized", UnfixedSelectors);
|
||
_objc_inform("PREOPTIMIZATION: %u/%u (%.3g%%) method lists pre-sorted",
|
||
PreoptOptimizedMethodLists, PreoptTotalMethodLists,
|
||
PreoptTotalMethodLists
|
||
? 100.0*PreoptOptimizedMethodLists/PreoptTotalMethodLists
|
||
: 0.0);
|
||
_objc_inform("PREOPTIMIZATION: %u/%u (%.3g%%) classes pre-registered",
|
||
PreoptOptimizedClasses, PreoptTotalClasses,
|
||
PreoptTotalClasses
|
||
? 100.0*PreoptOptimizedClasses/PreoptTotalClasses
|
||
: 0.0);
|
||
_objc_inform("PREOPTIMIZATION: %zu protocol references not "
|
||
"pre-optimized", UnfixedProtocolReferences);
|
||
}
|
||
|
||
#undef EACH_HEADER
|
||
}
|
||
```
|
||
|
||
可以看到内部有 `Discover categories` 相关逻辑,里面和 category 方法相关的有 `remethodizeClass`,其实现如下
|
||
|
||
```c
|
||
static void remethodizeClass(Class cls){
|
||
category_list *cats;
|
||
bool isMeta;
|
||
runtimeLock.assertWriting();
|
||
isMeta = cls->isMetaClass();
|
||
// Re-methodizing: check for more categories
|
||
if ((cats = unattachedCategoriesForClass(cls, false/*not realizing*/))) {
|
||
if (PrintConnecting) {
|
||
_objc_inform("CLASS: attaching categories to class '%s' %s",
|
||
cls->nameForLogging(), isMeta ? "(meta)" : "");
|
||
}
|
||
attachCategories(cls, cats, true /*flush caches*/);
|
||
free(cats);
|
||
}
|
||
}
|
||
```
|
||
|
||
可以看到内部调用 `attachCategories` 方法。 `attachCategories` 方法传入 3个参数,第一个是类对象(Person),第二个参数是 Category 数组。内部实现如下
|
||
|
||
```c++
|
||
static void attachCategories(Class cls, category_list *cats, bool flush_caches){
|
||
if (!cats) return;
|
||
if (PrintReplacedMethods) printReplacements(cls, cats);
|
||
|
||
bool isMeta = cls->isMetaClass();
|
||
// fixme rearrange to remove these intermediate allocations
|
||
method_list_t **mlists = (method_list_t **)
|
||
malloc(cats->count * sizeof(*mlists));
|
||
property_list_t **proplists = (property_list_t **)
|
||
malloc(cats->count * sizeof(*proplists));
|
||
protocol_list_t **protolists = (protocol_list_t **)
|
||
malloc(cats->count * sizeof(*protolists));
|
||
|
||
// Count backwards through cats to get newest categories first
|
||
int mcount = 0;
|
||
int propcount = 0;
|
||
int protocount = 0;
|
||
int i = cats->count;
|
||
bool fromBundle = NO;
|
||
while (i--) {
|
||
auto& entry = cats->list[i];
|
||
|
||
method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
|
||
if (mlist) {
|
||
mlists[mcount++] = mlist;
|
||
fromBundle |= entry.hi->isBundle();
|
||
}
|
||
|
||
property_list_t *proplist =
|
||
entry.cat->propertiesForMeta(isMeta, entry.hi);
|
||
if (proplist) {
|
||
proplists[propcount++] = proplist;
|
||
}
|
||
|
||
protocol_list_t *protolist = entry.cat->protocols;
|
||
if (protolist) {
|
||
protolists[protocount++] = protolist;
|
||
}
|
||
}
|
||
|
||
auto rw = cls->data();
|
||
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
|
||
rw->methods.attachLists(mlists, mcount);
|
||
free(mlists);
|
||
if (flush_caches && mcount > 0) flushCaches(cls);
|
||
|
||
rw->properties.attachLists(proplists, propcount);
|
||
free(proplists);
|
||
|
||
rw->protocols.attachLists(protolists, protocount);
|
||
free(protolists);
|
||
}
|
||
```
|
||
|
||
观察到采用 `i--` 的方式,当 while 循环结束的时候,方法数组 `mlists` 保存了全部分类中的方法,属性数组 `proplists` 保存了全部分类中的属性,协议数组 `protolists` 保存了所有分类所遵循的协议。
|
||
|
||
可以看到通过传入的类对象 `cls` 调用其 `cls->data()` 方法,找到对应的类 `class_rw_t` 信息,里面存放:方法列表、属性列表、协议列表信息。
|
||
|
||
```c
|
||
// 类对象结构体
|
||
struct objc_class : objc_object {
|
||
// Class ISA;
|
||
Class superclass;
|
||
}cache_t cache; // formerly cache pointer and vtable
|
||
class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
|
||
|
||
class_rw_t *data() {
|
||
return bits.data();
|
||
}
|
||
}
|
||
|
||
struct class_rw_t {
|
||
uint32_t flags;
|
||
uint32_t version;
|
||
const class_ro_t *ro;
|
||
method_array_t methods;
|
||
property_array_t properties;
|
||
protocol_array_t protocols;
|
||
|
||
class_rw_t* data() {
|
||
return (class_rw_t *)(bits & FAST_DATA_MASK);
|
||
}
|
||
}
|
||
```
|
||
|
||
可以看到最后调用 `attachLists`,内部实现如下
|
||
|
||
```c
|
||
void attachLists(List* const * addedLists, uint32_t addedCount) {
|
||
if (addedCount == 0) return;
|
||
|
||
if (hasArray()) {
|
||
// many lists -> many lists
|
||
uint32_t oldCount = array()->count;
|
||
uint32_t newCount = oldCount + addedCount;
|
||
setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
|
||
array()->count = newCount;
|
||
memmove(array()->lists + addedCount, array()->lists,
|
||
oldCount * sizeof(array()->lists[0]));
|
||
memcpy(array()->lists, addedLists,
|
||
addedCount * sizeof(array()->lists[0]));
|
||
}
|
||
else if (!list && addedCount == 1) {
|
||
// 0 lists -> 1 list
|
||
list = addedLists[0];
|
||
}
|
||
else {
|
||
// 1 list -> many lists
|
||
List* oldList = list;
|
||
uint32_t oldCount = oldList ? 1 : 0;
|
||
uint32_t newCount = oldCount + addedCount;
|
||
setArray((array_t *)malloc(array_t::byteSize(newCount)));
|
||
array()->count = newCount;
|
||
if (oldList) array()->lists[addedCount] = oldList;
|
||
memcpy(array()->lists, addedLists,
|
||
addedCount * sizeof(array()->lists[0]));
|
||
}
|
||
}
|
||
```
|
||
|
||
其中关键函数 `memmove` 代表将 __src 中的前 __len 个字节长度移动到 __dst 中去。
|
||
|
||
```c++
|
||
memmove(array()->lists + addedCount, array()->lists,
|
||
oldCount * sizeof(array()->lists[0]));
|
||
memcpy(array()->lists, addedLists,
|
||
addedCount * sizeof(array()->lists[0]));
|
||
```
|
||
|
||
等价于
|
||
|
||
```c++
|
||
memmove(类对象原来的方法列表 + addedCount,
|
||
类对象原来的方法列表,
|
||
oldCount * sizeof(array()->lists[0]))
|
||
|
||
memcopy(类对象原来的方法列表,
|
||
所有分类的方法列表,
|
||
addedCount * sizeof(array()->lists[0]))
|
||
```
|
||
|
||
其中,`array()->lists` 代表类对象原来的方法列表、`oldCount * sizeof(array()->lists[0])` 代表类对象原来方法列表长度,`addedCount` 代表 category 方法列表长度。
|
||
|
||
c 数组指针 `array()->lists + addedCount` 可以代表其中的位置。
|
||
|
||
`memmove` 的效果是,将类原来的方法列表移动到第 n个(n为 category 方法列表长度位置,前面空出n个坑位,预留坑位给所有分类的方法)
|
||
|
||
`memcopy` 效果将 Category 方法列表拷贝到类原方法列表的前面去。位置刚好是 `memmove` 留出的坑位。
|
||
|
||
过程如下
|
||
|
||

|
||
|
||
结果就是类方法列表中,最前面的就是所有分类的方法列表,最后是类自身的方法列表。
|
||
|
||
总结:
|
||
|
||
Category 编译之后 底层结构为 struct category_t,里面存储着分类的对象方法、类方法、属性、协议信息
|
||
|
||
程序运行的时候,runtime 会将 Category 中的数据,合并到类自身信息中(类对象、元类对象)
|
||
|
||
|
||
|
||
### QA
|
||
|
||
#### 分类中可以写属性吗?
|
||
|
||
不可以。从源码角度来讲,查看分类的 category_t 结构体可以看到没有 `const ivar_list_t * ivars;` ,所以 category 声明属性底层只会生成 setter、getter 方法声明,没有实现。需要程序员利用 runtime 关联属性自己实现
|
||
|
||
同理,分类中也不可以添加成员变量,下面代码会报错。
|
||
|
||
```objective-c
|
||
@interface Person (Study)<NSCopying>
|
||
{
|
||
int _age;
|
||
}
|
||
@end
|
||
```
|
||
|
||
|
||
|
||
从代码设计角度来讲,假设一个 Person 类只有1个 `_age` 成员变量,其内存布局在编译阶段就可以确定。内存布局大概为:
|
||
|
||
```objective-c
|
||
struct Person_IMPL {
|
||
Class isa;
|
||
int _age;
|
||
}
|
||
```
|
||
|
||
但 Category 是苹果利用 Runtime 实现的,是运行期动态修改 `class_rw_t` 决定的。
|
||
|
||
|
||
|
||
#### 为什么分类中的方法需要放在类自身方法列表的开头?
|
||
|
||
查看源码为什么分类中的方法需要放在类自身方法列表的开头?因为需要优先保证 Category 中的方法优先被调用。
|
||
|
||
|
||
|
||
#### 分类中存在同名方法存在什么问题
|
||
|
||
对象调用方法的时候会根据对象的 isa 指针,找到类对象方法列表,然后查找方法,由于分类方法在方法列表的前面,类自身方法在方法列表的后面,所以当优先找到分类方法实现的时候就停止查找了,给人的感受就是,方法”被覆盖了 “
|
||
|
||
Demo: 为 Person 类创建2个 Category,分别存在同名方法 study,具有不同实现。在控制器的手势事件中打印方法列表
|
||
|
||
```objective-c
|
||
- (void)displayMethodName:(Class)cls {
|
||
unsigned int count;
|
||
Method *methodList = class_copyMethodList(cls, &count);
|
||
NSMutableString *methodNames = [NSMutableString string];
|
||
for (int i = 0; i < count; i++) {
|
||
Method method = methodList[i];
|
||
NSString *methodName = NSStringFromSelector(method_getName(method));
|
||
[methodNames appendString:methodName];
|
||
[methodNames appendString:@", "];
|
||
}
|
||
free(methodList);
|
||
NSLog(@"className: %@, methodNames: %@", NSStringFromClass(cls) , methodNames);
|
||
}
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
self.p = [[Person alloc] init];
|
||
}
|
||
|
||
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
||
[self.p study];
|
||
[self displayMethodName:[Person class]];
|
||
}
|
||
```
|
||
|
||
<img src="https://github.com/FantasticLBP/knowledge-kit/raw/master/assets/OCCategoryMethodOrderExplore.png" style="zoom:25%">
|
||
|
||
可以看到 sayHi 方法存在多个,但是由于 Category 同名的方法在方法列表的前面,所以类自身的方法实现”被覆盖了“(根据 isa 查找方法实现的时候,优先查找到 Category 的方法实现,则停止查找了)
|
||
|
||
|
||
|
||
#### 2个分类存在同名方法,谁先调用
|
||
|
||
- 分类方法优先级高于类自身方法
|
||
- 同样是分类方法,由编译顺序决定哪个方法会被调用(Xcode:Build Phases -> Compile Sources),编译顺序越后面的方法优先被调用
|
||
|
||
Demo: 为 Person 类创建2个 Category,分别存在同名方法 study,具有不同实现。探索编译顺序决定方法实现
|
||
|
||
<img src="https://github.com/FantasticLBP/knowledge-kit/raw/master/assets/OCCategoryBuildOrderDemo1.png" style="zoom:25%">
|
||
|
||
2个对比实验:
|
||
|
||
让 `Person+Study` 参与后编译
|
||
|
||
<img src="https://github.com/FantasticLBP/knowledge-kit/raw/master/assets/OCCategoryBuildOrderDemo2.png" style="zoom:25%">
|
||
|
||
让 `Person+Learn` 参与后编译
|
||
|
||
<img src="https://github.com/FantasticLBP/knowledge-kit/raw/master/assets/OCCategoryBuildOrderDemo3.png" style="zoom:25%">
|
||
|
||
|
||
|
||
## 拓展(Extension)
|
||
|
||
### 文件特征
|
||
|
||
- 只存在一个文件
|
||
|
||
- 命名方式:“类名_拓展名.h”
|
||
|
||
```
|
||
#import "类名.h"
|
||
@interface 类名 ()
|
||
// 在此添加私有成员变量、属性、声明方法
|
||
@end
|
||
```
|
||
|
||
### 拓展的作用
|
||
|
||
1. 为类增加额外的属性、成员变量、方法声明
|
||
|
||
2. 一般将类拓展直接写到当前类的 .m 文件中。不单独创建
|
||
|
||
3. 一般私有的属性和方法写到类拓展中
|
||
|
||
4. 和 Category 类似,但是小括号里面没有拓展的名字
|
||
|
||
5. 拓展里面的属性和方法,会在编译阶段将相关数据和类本身合并
|
||
|
||
### 拓展的局限性
|
||
|
||
1. Extension 中添加的属性、成员变量、方法属于私有(只可以在本类的 .m 文件中访问、调用。在其他类里面是无法访问的,同时子类也是无法继承的)。假如我们有这样一个需求,一个属性对外是只读的,对内是可以读写的,那么我们可以通过 Extension 实现。
|
||
|
||
2. 通常 Extension 都写在 .m 文件中,不会单独建立一个 Extension 文件。而且 Extension 必须写到 @implementation 上方,否则编译报错
|
||
|
||
3. 类拓展定义的方法和属性必须在类的实现文件中实现。如果单独定义类扩展的文件并且只定义属性的话,也需要将类实现文件中包含进类扩展文件,否则会找不到属性的 setter 和 getter 方法。
|
||
|
||
```objectivec
|
||
//Web.h
|
||
#import "Person.h"
|
||
NS_ASSUME_NONNULL_BEGIN
|
||
@interface Web : Person
|
||
@end
|
||
NS_ASSUME_NONNULL_END
|
||
|
||
//Web.m
|
||
#import "Web.h"
|
||
#import "Web+H5.h"
|
||
@interface Web ()
|
||
@property (nonatomic, strong) NSString *skillStacks;
|
||
@end
|
||
@implementation Web
|
||
|
||
- (void)test {
|
||
self.skills = @"iOS && Web && Node && Hybrid";
|
||
self.skillStacks = @"iOS && Web && Node && Hybrid";
|
||
}
|
||
- (void)show {
|
||
NSLog(@"%@",self.skillStacks);
|
||
}
|
||
@end
|
||
```
|
||
|
||
## 总结
|
||
|
||
1. Category 只能拓充方法,不能拓展属性和成员变量(包含成员变量会报错。属性虽然不可以直接拓展,利用 Runtime 可以实现)
|
||
|
||
2. 如果 Category 中声明了1个属性,那么 Category 只会生成 setter 和 getter 的声明,不会有实现
|
||
|
||
3. Extension 也被成为匿名的 Category
|
||
|
||
4. 分类的方法本质是追加在当前类方法列表后,所以分类的方法会覆盖当前类的方法。
|
||
|
||
关于第4点,我们可以查看源代码印证下。去 opensource 下载 objc4
|
||
|
||
OC 入口函数`_objc_init`
|
||
|
||
```objectivec
|
||
void _objc_init(void)
|
||
{
|
||
// ...
|
||
_dyld_objc_notify_register(&map_images, load_images, unmap_image);
|
||
}
|
||
```
|
||
|
||
之后注册各种镜像,那么 map_images 哪里来的?
|
||
|
||
```objectivec
|
||
void
|
||
map_images_nolock(unsigned mhCount, const char * const mhPaths[],
|
||
const struct mach_header * const mhdrs[])
|
||
{
|
||
// ...
|
||
if (hCount > 0) {
|
||
_read_images(hList, hCount, totalClasses, unoptimizedTotalClasses);
|
||
}
|
||
firstTime = NO;
|
||
}
|
||
```
|
||
|
||
_read_images 方法内部会调用 remethodizeClass
|
||
|
||
```objectivec
|
||
void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses)
|
||
{
|
||
// ...
|
||
if (cls->isRealized()) {
|
||
remethodizeClass(cls);
|
||
// ...
|
||
}
|
||
```
|
||
|
||
remethodizeClass 内部会调用 attachCategories
|
||
|
||
```objectivec
|
||
static void remethodizeClass(Class cls)
|
||
{
|
||
category_list *cats;
|
||
bool isMeta;
|
||
|
||
runtimeLock.assertWriting();
|
||
|
||
isMeta = cls->isMetaClass();
|
||
|
||
// Re-methodizing: check for more categories
|
||
if ((cats = unattachedCategoriesForClass(cls, false/*not realizing*/))) {
|
||
if (PrintConnecting) {
|
||
_objc_inform("CLASS: attaching categories to class '%s' %s",
|
||
cls->nameForLogging(), isMeta ? "(meta)" : "");
|
||
}
|
||
|
||
attachCategories(cls, cats, true /*flush caches*/);
|
||
free(cats);
|
||
}
|
||
}
|
||
```
|
||
|
||
attachCategories 会调用 attachLists
|
||
|
||
```objectivec
|
||
static void
|
||
attachCategories(Class cls, category_list *cats, bool flush_caches)
|
||
{
|
||
if (!cats) return;
|
||
if (PrintReplacedMethods) printReplacements(cls, cats);
|
||
|
||
bool isMeta = cls->isMetaClass();
|
||
|
||
// fixme rearrange to remove these intermediate allocations
|
||
method_list_t **mlists = (method_list_t **)
|
||
malloc(cats->count * sizeof(*mlists));
|
||
property_list_t **proplists = (property_list_t **)
|
||
malloc(cats->count * sizeof(*proplists));
|
||
protocol_list_t **protolists = (protocol_list_t **)
|
||
malloc(cats->count * sizeof(*protolists));
|
||
|
||
// Count backwards through cats to get newest categories first
|
||
int mcount = 0;
|
||
int propcount = 0;
|
||
int protocount = 0;
|
||
int i = cats->count;
|
||
bool fromBundle = NO;
|
||
while (i--) {
|
||
auto& entry = cats->list[i];
|
||
|
||
method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
|
||
if (mlist) {
|
||
mlists[mcount++] = mlist;
|
||
fromBundle |= entry.hi->isBundle();
|
||
}
|
||
|
||
property_list_t *proplist =
|
||
entry.cat->propertiesForMeta(isMeta, entry.hi);
|
||
if (proplist) {
|
||
proplists[propcount++] = proplist;
|
||
}
|
||
|
||
protocol_list_t *protolist = entry.cat->protocols;
|
||
if (protolist) {
|
||
protolists[protocount++] = protolist;
|
||
}
|
||
}
|
||
|
||
auto rw = cls->data();
|
||
|
||
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
|
||
rw->methods.attachLists(mlists, mcount);
|
||
free(mlists);
|
||
if (flush_caches && mcount > 0) flushCaches(cls);
|
||
|
||
rw->properties.attachLists(proplists, propcount);
|
||
free(proplists);
|
||
|
||
rw->protocols.attachLists(protolists, protocount);
|
||
free(protolists);
|
||
}
|
||
```
|
||
|
||
attachLists 内部会调用 realloc、memmove、memmcpy
|
||
|
||
```objectivec
|
||
void attachLists(List* const * addedLists, uint32_t addedCount) {
|
||
if (addedCount == 0) return;
|
||
|
||
if (hasArray()) {
|
||
// many lists -> many lists
|
||
uint32_t oldCount = array()->count;
|
||
uint32_t newCount = oldCount + addedCount;
|
||
setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
|
||
array()->count = newCount;
|
||
memmove(array()->lists + addedCount, array()->lists,
|
||
oldCount * sizeof(array()->lists[0]));
|
||
memcpy(array()->lists, addedLists,
|
||
addedCount * sizeof(array()->lists[0]));
|
||
}
|
||
else if (!list && addedCount == 1) {
|
||
// 0 lists -> 1 list
|
||
list = addedLists[0];
|
||
}
|
||
else {
|
||
// 1 list -> many lists
|
||
List* oldList = list;
|
||
uint32_t oldCount = oldList ? 1 : 0;
|
||
uint32_t newCount = oldCount + addedCount;
|
||
setArray((array_t *)malloc(array_t::byteSize(newCount)));
|
||
array()->count = newCount;
|
||
if (oldList) array()->lists[addedCount] = oldList;
|
||
memcpy(array()->lists, addedLists,
|
||
addedCount * sizeof(array()->lists[0]));
|
||
}
|
||
}
|
||
```
|
||
|
||
最后会把类对象、元类对象、分类二元数组整体处理,结果为最后编译的分类在整合后数组的最前面,也就是为什么说分类和原类存在同名方法时,会被覆盖,且最后编译的分类的方法实现是会被调用的原因。
|
||
|
||
- 通过 Runtime 加载某个类所有的 Category
|
||
|
||
- 所有的 Category 方法、属性、协议数据,合并到一个大数组中,后面参与编译的 Category 数据,会放在数组前面
|
||
|
||
- 合并后的 Category 数据(属性、方法、协议)插入到类原来数据的前面(比如class_rw_t->methods)
|
||
|
||
|
||
|
||
## 为 Category 实现属性的 Setter 和 Getter
|
||
|
||
```objectivec
|
||
#import "Person.h"
|
||
|
||
NS_ASSUME_NONNULL_BEGIN
|
||
|
||
@interface Person (Student)
|
||
|
||
|
||
/**< 学号*/
|
||
@property (nonatomic, strong) NSString *studyNumber;
|
||
|
||
@end
|
||
|
||
NS_ASSUME_NONNULL_END
|
||
|
||
|
||
#import "Person+Student.h"
|
||
#import <objc/message.h>
|
||
|
||
@implementation Person (Student)
|
||
|
||
- (void)sayHi {
|
||
NSLog(@"大家好,我叫%@,我今年%zd岁了",self.name,self.age);
|
||
}
|
||
|
||
|
||
/*
|
||
* 传统的做法是在 setter 里面这样写
|
||
_studyNumber = studyNumber;
|
||
ARC 自动管理内存
|
||
MRC
|
||
[_studyNumber release];
|
||
[studyNumber retain];
|
||
_studyNumber = studyNumber;
|
||
|
||
但是在 Category里面不会生成对应的实例变量,因此我们可以利用 Runtime 为我们的 category 关联属性的值
|
||
setter:objc_setAssociatedObject(self, @selector(firstView), firstView, OBJC_ASSOCIATION_RETAIN);
|
||
getter:objc_getAssociatedObject(self, @selector(firstView));
|
||
}
|
||
*/
|
||
- (void)setStudyNumber:(NSString *)studyNumber {
|
||
objc_setAssociatedObject(self, @selector(studyNumber), studyNumber
|
||
, OBJC_ASSOCIATION_RETAIN);
|
||
}
|
||
|
||
//@selector(studyNumber)
|
||
- (NSString *)studyNumber {
|
||
return objc_getAssociatedObject(self, @selector(studyNumber));
|
||
}
|
||
|
||
@end
|
||
```
|
||
|
||
说明: `objc_setAssociatedObject` 的第二个参数是`const void * _Nonnull key` 所以可以用 "studyNumber" 或者利用 `@selector()` 的特性返回的数据类型也满足,所以示例代码选用第二种方式
|
||
|
||
给分类添加属性的时候,为了避免多人开发对于属性添加造成的覆盖,我们需要为属性起一个独特的名字。比如我们的工程是组件化、模块化开展的工程,那么我们可以为属性命名的时候在前面添加当前模块的前缀。
|
||
|
||
比如我们在 Login-Register-Module 模块为 NSURL 的 Category 添加一个 title 的属性的时候,可以这样命名 LR_Title。请查看下面的代码
|
||
|
||
```Objective-c
|
||
#import <Foundation/Foundation.h>
|
||
|
||
NS_ASSUME_NONNULL_BEGIN
|
||
|
||
@interface NSURL (Title)
|
||
|
||
@property (nonatomic, copy) NSString *LR_title;
|
||
|
||
@end
|
||
|
||
NS_ASSUME_NONNULL_END
|
||
|
||
#import "NSURL+Title.h"
|
||
#import <objc/runtime.h>
|
||
|
||
@implementation NSURL (Title)
|
||
|
||
- (void)setLR_title:(NSString *)LR_title
|
||
{
|
||
objc_setAssociatedObject(self, @selector(LR_title), LR_title
|
||
, OBJC_ASSOCIATION_RETAIN);
|
||
}
|
||
|
||
- (NSString *)LR_title
|
||
{
|
||
return objc_getAssociatedObject(self, @selector(LR_title));
|
||
}
|
||
|
||
@end
|
||
```
|
||
|
||
|
||
|
||
## 底层剖析 load 方法
|
||
|
||
Demo 验证。
|
||
|
||
```objectivec
|
||
@interface Person : NSObject
|
||
@end
|
||
|
||
@interface Student : Person
|
||
@end
|
||
|
||
@interface Student (Good)
|
||
@end
|
||
|
||
@interface Student (Bad)
|
||
@end
|
||
// 其中每个类都存在3个方法
|
||
+ (void)load{
|
||
NSLog(@"%s", __func__);
|
||
}
|
||
+ (void)initialize{
|
||
NSLog(@"%s", __func__);
|
||
}
|
||
- (void)test{
|
||
NSLog(@"%s", __func__);
|
||
}
|
||
// Test
|
||
Student *st = [[Student alloc] init];
|
||
|
||
2022-04-16 01:35:22.237692+0800 Main[8752:2908124] +[Person load]
|
||
2022-04-16 01:35:22.238305+0800 Main[8752:2908124] +[Student load]
|
||
2022-04-16 01:35:22.238450+0800 Main[8752:2908124] +[Student(Good) load]
|
||
2022-04-16 01:35:22.238562+0800 Main[8752:2908124] +[Student(Bad) load]
|
||
2022-04-16 01:35:22.238664+0800 Main[8752:2908124] +[Person initialize]
|
||
2022-04-16 01:35:22.238733+0800 Main[8752:2908124] +[Student(Bad) initialize]
|
||
2022-04-16 01:35:22.238794+0800 Main[8752:2908124] -[Student(Bad) test]
|
||
```
|
||
|
||
QA:
|
||
|
||
- 为什么 `initialize`方法存在“覆盖”的情况?
|
||
|
||
就是打印了 Student Category 的 `initialize`,却没有打印 Student 自身的 `initialize`。
|
||
|
||
查看 objc 源码发现调用 `initialize` 方法本质上就是通过 runtime 的 `objc_msgSend ` 发消息来实现的。也就是通过 isa 指针查看类对象或元类对象的方法列表中(方法列表中包含当前类各个 Category 的各个方法)查找方法实现。所以当找到方法列表中排列较前的 `initialize` 时,就不再继续查找方法实现了。也就出现了 `initialize` 被覆盖的情况了。
|
||
|
||
```c++
|
||
void callInitialize(Class cls)
|
||
{
|
||
((void(*)(Class, SEL))objc_msgSend)(cls, @selector(initialize));
|
||
asm("");
|
||
}
|
||
```
|
||
|
||
- 为什么 load 方法打印顺序是这样的?
|
||
|
||
因为调用 student alloc,相当于发送了消息。则肯定先执行 load 方法。类在 Runtime 启动阶段会调用 `schedule_class_load` 方法。方法内部递归调用,如果当前类存在父类则递归调用,否则将当前类加载到 loadable_classes 最后面。load 方法在本质上是执行 `call_load_methods`,方法地址是确定的(查看下面的源代码可以发现 `load` 方法是在编译期就可以确定的)。不走 `objc_msgSend` 这套流程。所以先打印父类 load、再打印子类 load、最后打印分类 load。如果存在多个分类,则按照编译顺序打印 load。
|
||
|
||
- 为什么 load 方法不是按照 Category 编译顺序倒序调用 load 方法?
|
||
|
||
看源代码 Objc4
|
||
|
||
```c
|
||
void _objc_init(void){
|
||
static bool initialized = false;
|
||
if (initialized) return;
|
||
initialized = true;
|
||
|
||
// fixme defer initialization until an objc-using image is found?
|
||
environ_init();
|
||
tls_init();
|
||
static_init();
|
||
lock_init();
|
||
exception_init();
|
||
|
||
_dyld_objc_notify_register(&map_images, load_images, unmap_image);
|
||
}
|
||
|
||
void load_images(const char *path __unused, const struct mach_header *mh){
|
||
// Return without taking locks if there are no +load methods here.
|
||
if (!hasLoadMethods((const headerType *)mh)) return;
|
||
recursive_mutex_locker_t lock(loadMethodLock);
|
||
// Discover load methods
|
||
{
|
||
rwlock_writer_t lock2(runtimeLock);
|
||
prepare_load_methods((const headerType *)mh);
|
||
}
|
||
// Call +load methods (without runtimeLock - re-entrant)
|
||
call_load_methods();
|
||
}
|
||
|
||
void call_load_methods(void){
|
||
static bool loading = NO;
|
||
bool more_categories;
|
||
loadMethodLock.assertLocked();
|
||
// Re-entrant calls do nothing; the outermost call will finish the job.
|
||
if (loading) return;
|
||
loading = YES;
|
||
|
||
void *pool = objc_autoreleasePoolPush();
|
||
|
||
do {
|
||
// 1. Repeatedly call class +loads until there aren't any more
|
||
while (loadable_classes_used > 0) {
|
||
call_class_loads(); // 先调用类的 load 方法
|
||
}
|
||
|
||
// 2. Call category +loads ONCE
|
||
more_categories = call_category_loads(); // 再调用 category 的 load 方法
|
||
|
||
// 3. Run more +loads if there are classes OR more untried categories
|
||
} while (loadable_classes_used > 0 || more_categories);
|
||
|
||
objc_autoreleasePoolPop(pool);
|
||
|
||
loading = NO;
|
||
}
|
||
|
||
static void call_class_loads(void) {
|
||
int i;
|
||
// Detach current loadable list.
|
||
struct loadable_class *classes = loadable_classes;
|
||
int used = loadable_classes_used;
|
||
loadable_classes = nil;
|
||
loadable_classes_allocated = 0;
|
||
loadable_classes_used = 0;
|
||
|
||
// Call all +loads for the detached list.
|
||
for (i = 0; i < used; i++) {
|
||
Class cls = classes[i].cls;
|
||
load_method_t load_method = (load_method_t)classes[i].method;
|
||
if (!cls) continue;
|
||
|
||
if (PrintLoading) {
|
||
_objc_inform("LOAD: +[%s load]\n", cls->nameForLogging());
|
||
}
|
||
(*load_method)(cls, SEL_load);
|
||
}
|
||
|
||
// Destroy the detached list.
|
||
if (classes) free(classes);
|
||
}
|
||
|
||
static bool call_category_loads(void) {
|
||
int i, shift;
|
||
bool new_categories_added = NO;
|
||
// Detach current loadable list.
|
||
struct loadable_category *cats = loadable_categories;
|
||
int used = loadable_categories_used;
|
||
int allocated = loadable_categories_allocated;
|
||
loadable_categories = nil;
|
||
loadable_categories_allocated = 0;
|
||
loadable_categories_used = 0;
|
||
|
||
// Call all +loads for the detached list.
|
||
for (i = 0; i < used; i++) {
|
||
Category cat = cats[i].cat;
|
||
load_method_t load_method = (load_method_t)cats[i].method;
|
||
Class cls;
|
||
if (!cat) continue;
|
||
|
||
cls = _category_getClass(cat);
|
||
if (cls && cls->isLoadable()) {
|
||
if (PrintLoading) {
|
||
_objc_inform("LOAD: +[%s(%s) load]\n",
|
||
cls->nameForLogging(),
|
||
_category_getName(cat));
|
||
}
|
||
(*load_method)(cls, SEL_load);
|
||
cats[i].cat = nil;
|
||
}
|
||
}
|
||
|
||
// Compact detached list (order-preserving)
|
||
shift = 0;
|
||
for (i = 0; i < used; i++) {
|
||
if (cats[i].cat) {
|
||
cats[i-shift] = cats[i];
|
||
} else {
|
||
shift++;
|
||
}
|
||
}
|
||
used -= shift;
|
||
|
||
// Copy any new +load candidates from the new list to the detached list.
|
||
new_categories_added = (loadable_categories_used > 0);
|
||
for (i = 0; i < loadable_categories_used; i++) {
|
||
if (used == allocated) {
|
||
allocated = allocated*2 + 16;
|
||
cats = (struct loadable_category *)
|
||
realloc(cats, allocated *
|
||
sizeof(struct loadable_category));
|
||
}
|
||
cats[used++] = loadable_categories[i];
|
||
}
|
||
|
||
// Destroy the new list.
|
||
if (loadable_categories) free(loadable_categories);
|
||
|
||
// Reattach the (now augmented) detached list.
|
||
// But if there's nothing left to load, destroy the list.
|
||
if (used) {
|
||
loadable_categories = cats;
|
||
loadable_categories_used = used;
|
||
loadable_categories_allocated = allocated;
|
||
} else {
|
||
if (cats) free(cats);
|
||
loadable_categories = nil;
|
||
loadable_categories_used = 0;
|
||
loadable_categories_allocated = 0;
|
||
}
|
||
|
||
if (PrintLoading) {
|
||
if (loadable_categories_used != 0) {
|
||
_objc_inform("LOAD: %d categories still waiting for +load\n",
|
||
loadable_categories_used);
|
||
}
|
||
}
|
||
|
||
return new_categories_added;
|
||
}
|
||
```
|
||
|
||
会发现源码中先调用类的 load 方法,再调用 category 的 load 方法。
|
||
|
||
再看看 `call_class_loads`、`call_category_loads` 方法内部实现,是直接找到 `load_method_t load_method = (load_method_t)classes[i].method;` 类对象的 load 方法地址。最后直接调用 `(*load_method)(cls, SEL_load);` 方法本身。
|
||
|
||
test 方法是走消息发送流程 `objc_msgSend()` 所以会走 isa、superclass 这一套流程,test 是对象方法,所以需要根据 isa 找到类对象,从类对象的对象方法列表找到 test 方法,找不到则根据 superclass 找到当前类对象的父类对象,继续查找方法列表。直到 NSObject、nil 对象为止,然后走消息起死回生的阶段。
|
||
|
||
看2个结构体
|
||
|
||
```c
|
||
struct loadable_class {
|
||
Class cls; // may be nil
|
||
IMP method; // 指向类的 load 方法
|
||
};
|
||
|
||
struct loadable_category {
|
||
Category cat; // may be nil
|
||
IMP method; // 指向分类的 load 方法
|
||
};
|
||
```
|
||
|
||
类、分类的 load 方法调用顺序?
|
||
|
||
1. 调用类的 +load 方法顺序
|
||
|
||
- 调用类的 +load
|
||
|
||
- 根据编译先后顺序调用 +load(先编译先调用)
|
||
|
||
- 存在继承关系的类,会先调用父类的 +load
|
||
|
||
2. 调用分类的 +load 方法顺序
|
||
|
||
- 按照编译顺序调用分类的 +load(先编译先调用)
|
||
|
||
源代码印证
|
||
|
||
```objectivec
|
||
void load_images(const char *path __unused, const struct mach_header *mh) {
|
||
// Return without taking locks if there are no +load methods here.
|
||
if (!hasLoadMethods((const headerType *)mh)) return;
|
||
|
||
recursive_mutex_locker_t lock(loadMethodLock);
|
||
|
||
// Discover load methods
|
||
{
|
||
rwlock_writer_t lock2(runtimeLock);
|
||
prepare_load_methods((const headerType *)mh);
|
||
}
|
||
|
||
// Call +load methods (without runtimeLock - re-entrant)
|
||
call_load_methods();
|
||
}
|
||
|
||
void prepare_load_methods(const headerType *mhdr){
|
||
size_t count, i;
|
||
|
||
runtimeLock.assertWriting();
|
||
|
||
classref_t *classlist =
|
||
_getObjc2NonlazyClassList(mhdr, &count);
|
||
for (i = 0; i < count; i++) {
|
||
schedule_class_load(remapClass(classlist[i]));
|
||
}
|
||
|
||
category_t **categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);
|
||
for (i = 0; i < count; i++) {
|
||
category_t *cat = categorylist[i];
|
||
Class cls = remapClass(cat->cls);
|
||
if (!cls) continue; // category for ignored weak-linked class
|
||
realizeClass(cls);
|
||
assert(cls->ISA()->isRealized());
|
||
add_category_to_loadable_list(cat);
|
||
}
|
||
}
|
||
```
|
||
|
||
我们看看 `schedule_class_load` 方法。方法内部递归调用,如果当前类存在父类则递归调用,否则将当前类加载到 loadable_classes 最后面
|
||
|
||
```objectivec
|
||
static void schedule_class_load(Class cls)
|
||
{
|
||
if (!cls) return;
|
||
assert(cls->isRealized()); // _read_images should realize
|
||
|
||
if (cls->data()->flags & RW_LOADED) return;
|
||
|
||
// Ensure superclass-first ordering
|
||
schedule_class_load(cls->superclass);
|
||
|
||
add_class_to_loadable_list(cls);
|
||
cls->setInfo(RW_LOADED);
|
||
}
|
||
```
|
||
|
||
```objectivec
|
||
void add_class_to_loadable_list(Class cls)
|
||
{
|
||
IMP method;
|
||
|
||
loadMethodLock.assertLocked();
|
||
|
||
method = cls->getLoadMethod();
|
||
if (!method) return; // Don't bother if cls has no +load method
|
||
|
||
if (PrintLoading) {
|
||
_objc_inform("LOAD: class '%s' scheduled for +load",
|
||
cls->nameForLogging());
|
||
}
|
||
|
||
if (loadable_classes_used == loadable_classes_allocated) {
|
||
loadable_classes_allocated = loadable_classes_allocated*2 + 16;
|
||
loadable_classes = (struct loadable_class *)
|
||
realloc(loadable_classes,
|
||
loadable_classes_allocated *
|
||
sizeof(struct loadable_class));
|
||
}
|
||
|
||
loadable_classes[loadable_classes_used].cls = cls;
|
||
loadable_classes[loadable_classes_used].method = method; // 加载到最后
|
||
loadable_classes_used++;
|
||
}
|
||
```
|
||
|
||
`prepare_load_methods` 处理完再执行 `call_load_methods`
|
||
|
||
```objectivec
|
||
void call_load_methods(void)
|
||
{
|
||
static bool loading = NO;
|
||
bool more_categories;
|
||
|
||
loadMethodLock.assertLocked();
|
||
|
||
// Re-entrant calls do nothing; the outermost call will finish the job.
|
||
if (loading) return;
|
||
loading = YES;
|
||
|
||
void *pool = objc_autoreleasePoolPush();
|
||
|
||
do {
|
||
// 1. Repeatedly call class +loads until there aren't any more
|
||
while (loadable_classes_used > 0) {
|
||
call_class_loads();
|
||
}
|
||
|
||
// 2. Call category +loads ONCE
|
||
more_categories = call_category_loads();
|
||
|
||
// 3. Run more +loads if there are classes OR more untried categories
|
||
} while (loadable_classes_used > 0 || more_categories);
|
||
|
||
objc_autoreleasePoolPop(pool);
|
||
|
||
loading = NO;
|
||
}
|
||
这里
|
||
```
|
||
|
||
这里的代码已经看过了,也就先加载类的 +load 方法,加载顺序按照 loadable_classes 中的类顺序进行访问 +load。之后再加载 Catetory 的 +load 方法。
|
||
|
||
在 `prepare_load_methods` 方法内部先给普通类按照编译顺序(谁先编译谁先添加,遇到存在父类的类,递归调用父类对象)添加类信息到 loadable_classes 中,之后给分类按照编译顺序添加到(谁先编译谁先添加) loadable_categories 中。
|
||
|
||
+load 方法在 Runtime 加载类、分类的时候调用。
|
||
|
||
Extension 在编译阶段,数据已经包含在类信息中。
|
||
|
||
Category 是在运行阶段,才会将数据合并到类信息中。
|
||
|
||
|
||
|
||
## 底层剖析 Initialize 方法
|
||
|
||
上 Demo
|
||
|
||
```objectivec
|
||
@interface Person : NSObject
|
||
@end
|
||
|
||
@interface Student : Person
|
||
@end
|
||
|
||
@interface Student (Good)
|
||
@end
|
||
```
|
||
|
||
`Person *p1 = [[Person alloc] init];` 这句代码输出什么? 这个比较简单,initialize 方法在类第一次收到消息的时候调用。所以输出 `+[Person initialize]`
|
||
|
||
`Student *st = [[Student alloc] init];` 输出什么?
|
||
|
||
```objectivec
|
||
+[Person initialize]
|
||
+[Student(Good) initialize]
|
||
```
|
||
|
||
查看分类在 Runtime 加载类信息时候的调用原理可以知道,分类中的类方法、对象方法都会被加载原始类的前面去(initialize 是类方法)如下图:
|
||
|
||

|
||
|
||
|
||
|
||
`+initialize` 和 `+load` 最大区别是 `+initialize` 是通过 `objc_msgSend` 进行调用的
|
||
|
||
- 调用方式:`load` 根据函数地址直接调用,`initialize` 是根据 `objc_msgSend` 调用的
|
||
|
||
- 调用时刻:load 是 runtime 加载类、分类的时候调用的。initialize 是在类第一次接收消息的时候调用的。每个类只会 initialize 一次,但是父类的 initialize 可能会调用多次
|
||
|
||
- 调用顺序:
|
||
|
||
- load:先调用类的 load(先编译的类优先调用 load、调用子类的 load,会先调用父类的 load)、再调用分类的 load(先编译的分类,优先调用 load )
|
||
- 如果子类没有实现 `+initialize` 则会调用父类的 `+initialize`(所以父类的 `+initialize` 可能会被调用多次)
|
||
- 如果分类实现了 `+initialize`,就会覆盖类本身的 `+initialize` 调用
|
||
|
||
|
||
查看源码,伪代码如下:
|
||
|
||
```
|
||
if (自己没有初始化) {
|
||
if (父类没有初始化) {
|
||
objc_msgSend(父类,@selector(initializ))
|
||
}
|
||
objc_msgSend(子类,@selector(initializ))
|
||
}
|
||
```
|
||
|
||
```objectivec
|
||
void _class_initialize(Class cls)
|
||
{
|
||
assert(!cls->isMetaClass());
|
||
|
||
Class supercls;
|
||
bool reallyInitialize = NO;
|
||
|
||
// Make sure super is done initializing BEFORE beginning to initialize cls.
|
||
// See note about deadlock above.
|
||
supercls = cls->superclass;
|
||
if (supercls && !supercls->isInitialized()) {
|
||
_class_initialize(supercls);
|
||
}
|
||
|
||
// Try to atomically set CLS_INITIALIZING.
|
||
{
|
||
monitor_locker_t lock(classInitLock);
|
||
if (!cls->isInitialized() && !cls->isInitializing()) {
|
||
cls->setInitializing();
|
||
reallyInitialize = YES;
|
||
}
|
||
}
|
||
|
||
if (reallyInitialize) {
|
||
// We successfully set the CLS_INITIALIZING bit. Initialize the class.
|
||
|
||
// Record that we're initializing this class so we can message it.
|
||
_setThisThreadIsInitializingClass(cls);
|
||
|
||
if (MultithreadedForkChild) {
|
||
// LOL JK we don't really call +initialize methods after fork().
|
||
performForkChildInitialize(cls, supercls);
|
||
return;
|
||
}
|
||
|
||
// Send the +initialize message.
|
||
// Note that +initialize is sent to the superclass (again) if
|
||
// this class doesn't implement +initialize. 2157218
|
||
if (PrintInitializing) {
|
||
_objc_inform("INITIALIZE: thread %p: calling +[%s initialize]",
|
||
pthread_self(), cls->nameForLogging());
|
||
}
|
||
|
||
// Exceptions: A +initialize call that throws an exception
|
||
// is deemed to be a complete and successful +initialize.
|
||
//
|
||
// Only __OBJC2__ adds these handlers. !__OBJC2__ has a
|
||
// bootstrapping problem of this versus CF's call to
|
||
// objc_exception_set_functions().
|
||
#if __OBJC2__
|
||
@try
|
||
#endif
|
||
{
|
||
|
||
callInitialize(cls);
|
||
|
||
if (PrintInitializing) {
|
||
_objc_inform("INITIALIZE: thread %p: finished +[%s initialize]",
|
||
pthread_self(), cls->nameForLogging());
|
||
}
|
||
}
|
||
#if __OBJC2__
|
||
@catch (...) {
|
||
if (PrintInitializing) {
|
||
_objc_inform("INITIALIZE: thread %p: +[%s initialize] "
|
||
"threw an exception",
|
||
pthread_self(), cls->nameForLogging());
|
||
}
|
||
@throw;
|
||
}
|
||
@finally
|
||
#endif
|
||
{
|
||
// Done initializing.
|
||
lockAndFinishInitializing(cls, supercls);
|
||
}
|
||
return;
|
||
}
|
||
|
||
else if (cls->isInitializing()) {
|
||
// We couldn't set INITIALIZING because INITIALIZING was already set.
|
||
// If this thread set it earlier, continue normally.
|
||
// If some other thread set it, block until initialize is done.
|
||
// It's ok if INITIALIZING changes to INITIALIZED while we're here,
|
||
// because we safely check for INITIALIZED inside the lock
|
||
// before blocking.
|
||
if (_thisThreadIsInitializingClass(cls)) {
|
||
return;
|
||
} else if (!MultithreadedForkChild) {
|
||
waitForInitializeToComplete(cls);
|
||
return;
|
||
} else {
|
||
// We're on the child side of fork(), facing a class that
|
||
// was initializing by some other thread when fork() was called.
|
||
_setThisThreadIsInitializingClass(cls);
|
||
performForkChildInitialize(cls, supercls);
|
||
}
|
||
}
|
||
|
||
else if (cls->isInitialized()) {
|
||
// Set CLS_INITIALIZING failed because someone else already
|
||
// initialized the class. Continue normally.
|
||
// NOTE this check must come AFTER the ISINITIALIZING case.
|
||
// Otherwise: Another thread is initializing this class. ISINITIALIZED
|
||
// is false. Skip this clause. Then the other thread finishes
|
||
// initialization and sets INITIALIZING=no and INITIALIZED=yes.
|
||
// Skip the ISINITIALIZING clause. Die horribly.
|
||
return;
|
||
}
|
||
|
||
else {
|
||
// We shouldn't be here.
|
||
_objc_fatal("thread-safe class init in objc runtime is buggy!");
|
||
}
|
||
}
|
||
|
||
void callInitialize(Class cls) {
|
||
((void(*)(Class, SEL))objc_msgSend)(cls, SEL_initialize);
|
||
asm("");
|
||
}
|
||
```
|