Category的load方法什么時候調用医瘫?load方法能繼承嗎
- 在runtime加載類和分類的時候調用。類的load方法會先調用,然后才會按照編譯時的順序調用分類的load方法。
- 每個類钞诡、分類的+load方法,在程序運行過程中只調用一次
- 能繼承,繼承就是OC的消息發(fā)送機制荧降,通過isa指針接箫,再通過superclasss指針查找父類的方法。但一般不會主動調用+load方法朵诫,都是系統(tǒng)加載類的時候自動調用的
類和分類的+load方法的調用順序
- 先調用類的+load方法列牺,再調用分類的+load方法
- 類的+load方法會按照編譯順序調用,在調用+load方法的過程中拗窃,如果該類有父類瞎领,就會先調用該類父類的+load方法
- 分類的+load方法按照編譯順序進行調用
注:通過下面的源碼分析,根據(jù)prepare_load_methods
和schedule_class_load
可以得到上面的調用順序
Category load 方法源碼分析:
objc4-781版源碼
注意:+load方法是根據(jù)方法地址直接調用随夸,并不是進過objc_msgSend函數(shù)調用
源碼解讀過程:objc-os.mm 文件開始
- _objc_init 方法
load_images
: 加載鏡像(模塊)九默,其中會調用call_load_methods()
;
3.call_load_methods()
: 會先調用call_class_loads()
方法,再調用call_category_loads()
方法宾毒。所以會先調用類的+load
方法prepare_load_methods
:準備所有的load
方法驼修,會根據(jù)_getObjc2NonlazyClassLis
t這個數(shù)組,去循環(huán)調用schedule_class_load
方法诈铛。所以_getObjc2NonlazyClassList
順序乙各,就決定了類load
方法的調用順序。而_getObjc2NonlazyClassLis
t順序就直接跟編譯順序相關幢竹。
schedule_class_load
:安排配置load
方法耳峦,該函數(shù)會遞歸調用schedule_class_load(cls->superclass);
并把父類對象傳進入。所以父類對象會先添加到數(shù)組中焕毫。所以父類的load
會先調用蹲坷,再調用子類的load
方法add_class_to_loadable_list
:將class添加到數(shù)組中,add_category_to_loadable_list
:將category添加到數(shù)組中
call_load_methods
:調用load方法
call_class_loads
:調用所有類的load方法call_category_loads
:調用所有分類的load方法(*load_method)(cls,SEL_load)
:通過地址直接調用方法
先關源碼:
// 該結構專門用來加載類邑飒,method 存放的方法就是類的load方法
struct loadable_class {
Class cls; // may be nil
IMP method;
};
// 該結構專門用來加載分類循签,method 存放的方法就是分類類的load方法
struct loadable_category {
Category cat; // may be nil
IMP method;
};
load_images
void
load_images(const char *path __unused, const struct mach_header *mh)
{
if (!didInitialAttachCategories && didCallDyldNotifyRegister) {
didInitialAttachCategories = true;
loadAllCategories();
}
// 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
{
mutex_locker_t lock2(runtimeLock);
prepare_load_methods((const headerType *)mh);
}
// Call +load methods (without runtimeLock - re-entrant)
call_load_methods();
}
prepare_load_methods
void prepare_load_methods(const headerType *mhdr)
{
size_t count, i;
runtimeLock.assertLocked();
classref_t const *classlist =
_getObjc2NonlazyClassList(mhdr, &count);
for (i = 0; i < count; i++) {
schedule_class_load(remapClass(classlist[i]));
}
category_t * const *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
if (cls->isSwiftStable()) {
_objc_fatal("Swift class extensions and categories on Swift "
"classes are not allowed to have +load methods");
}
realizeClassWithoutSwift(cls, nil);
ASSERT(cls->ISA()->isRealized());
add_category_to_loadable_list(cat);
}
}
schedule_class_load
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);
}
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();
}
// 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;
}
call_class_loads
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方法的地址
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方法
(*load_method)(cls, @selector(load));
}
// Destroy the detached list.
if (classes) free(classes);
}
call_category_loads
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方法地址
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, @selector(load));
cats[i].cat = nil;
}
}
....