在平日編程中或閱讀第三方代碼時(shí)适滓,category
可以說是無處不在疹启。category
也可以說是OC作為一門動(dòng)態(tài)語言的一大特色辨泳。category
為我們動(dòng)態(tài)擴(kuò)展類的功能提供了可能谈截,或者我們也可以把一個(gè)龐大的類進(jìn)行功能分解,按照category
進(jìn)行組織芜辕。
關(guān)于category
的使用無需多言尚骄,今天我們來深入了解一下,category
是如何在runtime中實(shí)現(xiàn)的侵续。
category的數(shù)據(jù)結(jié)構(gòu)
category對應(yīng)到runtime中的結(jié)構(gòu)體是struct category_t(位于objc-runtime-new.h):
struct category_t {
const char *name;
classref_t cls;
struct method_list_t *instanceMethods;
struct method_list_t *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) {
if (isMeta) return classMethods;
else return instanceMethods;
}
property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi);
};
category_t
的定義很簡單倔丈。從定義中看出,category
的可為:添加實(shí)例方法(instanceMethods
)状蜗,類方法(classMethods
)需五,協(xié)議(protocols
)和實(shí)例屬性(instanceProperties
),以及不可為:不能夠添加實(shí)例變量(關(guān)于實(shí)例屬性和實(shí)例變量的區(qū)別轧坎,我們將會(huì)在別的章節(jié)中探討)宏邮。
category的加載
知道了category
的數(shù)據(jù)結(jié)構(gòu),我們來深入探究一下category
是如何在runtime
中實(shí)現(xiàn)的。
原理很簡單:runtime
會(huì)分別將category
結(jié)構(gòu)體中的instanceMethods
, protocols
蜜氨,instanceProperties
添加到target class
的實(shí)例方法列表械筛,協(xié)議列表,屬性列表中飒炎,會(huì)將category
結(jié)構(gòu)體中的classMethods
添加到target class
所對應(yīng)的元類
的實(shí)例方法列表中埋哟。其本質(zhì)就相當(dāng)于runtime
在運(yùn)行時(shí)期,修改了target class
的結(jié)構(gòu)郎汪。
經(jīng)過這一番修改赤赊,category
中的方法,就變成了target class
方法列表中的一部分怒竿,其調(diào)用方式也就一模一樣啦~
現(xiàn)在砍鸠,就來看一下具體是怎么實(shí)現(xiàn)的。
首先耕驰,我們在Mach-O格式和runtime 介紹過在Mach-O文件中爷辱,category數(shù)據(jù)會(huì)被存放在__DATA段下的__objc_catlist section中。
當(dāng)OC被dyld加載起來時(shí)朦肘,OC進(jìn)入其入口點(diǎn)函數(shù)_objc_init
:
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);
}
我們忽略一堆init方法饭弓,重點(diǎn)來看_dyld_objc_notify_register
方法。該方法會(huì)向dyld注冊監(jiān)聽Mach-O中OC相關(guān)section被加載入\載出內(nèi)存的事件媒抠。
具體有三個(gè)事件:
_dyld_objc_notify_mapped
(對應(yīng)&map_images回調(diào)):當(dāng)dyld已將OC images加載入內(nèi)存時(shí)弟断。
_dyld_objc_notify_init
(對應(yīng)load_images回調(diào)):當(dāng)dyld將要初始化OC image時(shí)。OC調(diào)用類的+load方法趴生,就是在這時(shí)進(jìn)行的阀趴。
_dyld_objc_notify_unmapped
(對應(yīng)unmap_image回調(diào)):當(dāng)dyld將OC images移除內(nèi)存時(shí)。
而category寫入target class的方法列表苍匆,則是在_dyld_objc_notify_mapped
刘急,即將OC相關(guān)sections都加載到內(nèi)存之后所發(fā)生的。
我們可以看到其對應(yīng)回調(diào)為map_images
方法浸踩。
在map_images
最終會(huì)調(diào)用_read_images
方法來讀取OC相關(guān)sections叔汁,并以此來初始化OC內(nèi)存環(huán)境。_read_images
的極簡實(shí)現(xiàn)版如下检碗,可以看到据块,rumtime是如何根據(jù)Mach-O各個(gè)section的信息來初始化其自身的:
void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses)
{
static bool doneOnce;
TimeLogger ts(PrintImageTimes);
runtimeLock.assertWriting();
if (!doneOnce) {
doneOnce = YES;
ts.log("IMAGE TIMES: first time tasks");
}
// Discover classes. Fix up unresolved future classes. Mark bundle classes.
for (EACH_HEADER) {
classref_t *classlist = _getObjc2ClassList(hi, &count);
for (i = 0; i < count; i++) {
Class cls = (Class)classlist[I];
Class newCls = readClass(cls, headerIsBundle, headerIsPreoptimized);
}
}
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.
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");
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);
}
}
ts.log("IMAGE TIMES: fix up selector references");
// 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;
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);
bool classExists = NO;
if (cat->instanceMethods || cat->protocols
|| cat->instanceProperties)
{
addUnattachedCategoryForClass(cat, cls, hi);
}
if (cat->classMethods || cat->protocols
|| (hasClassProperties && cat->_classProperties))
{
addUnattachedCategoryForClass(cat, cls->ISA(), hi);
}
}
}
ts.log("IMAGE TIMES: discover categories");
}
大致的邏輯是,runtime調(diào)用_getObjc2XXX
格式的方法折剃,依次來讀取對應(yīng)的section
內(nèi)容另假,并根據(jù)其結(jié)果初始化其自身結(jié)構(gòu)。
_getObjc2XXX
方法有如下幾種怕犁,可以看到他們都一一對應(yīng)了Mach-O中相關(guān)的OC seciton边篮。
// function name content type section name
GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs");
GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs");
GETSECT(_getObjc2ClassRefs, Class, "__objc_classrefs");
GETSECT(_getObjc2SuperRefs, Class, "__objc_superrefs");
GETSECT(_getObjc2ClassList, classref_t, "__objc_classlist");
GETSECT(_getObjc2NonlazyClassList, classref_t, "__objc_nlclslist");
GETSECT(_getObjc2CategoryList, category_t *, "__objc_catlist");
GETSECT(_getObjc2NonlazyCategoryList, category_t *, "__objc_nlcatlist");
GETSECT(_getObjc2ProtocolList, protocol_t *, "__objc_protolist");
GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs");
GETSECT(getLibobjcInitializers, Initializer, "__objc_init_func");
可以看到开睡,我們使用的類,協(xié)議和category苟耻,都是在_read_images
方法中讀取出來的。
在讀取cateogry的方法 _getObjc2CategoryList(hi, &count)
中扶檐,讀取的是Mach-O文件的 __objc_catlist
段凶杖。
我們重點(diǎn)關(guān)注和category相關(guān)的代碼:
// 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);
bool classExists = NO;
// 如果Category中有實(shí)例方法,協(xié)議款筑,實(shí)例屬性智蝠,會(huì)改寫target class的結(jié)構(gòu)
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" : "");
}
}
// 如果category中有類方法,協(xié)議奈梳,或類屬性(目前OC版本不支持類屬性), 會(huì)改寫target class的元類結(jié)構(gòu)
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");
discover categories的邏輯如下:
- 先調(diào)用
_getObjc2CategoryList
讀取__objc_catlist
seciton下所記錄的所有category
杈湾。并存放到category_t *
數(shù)組中。 - 依次讀取數(shù)組中的
category_t * cat
- 對每一個(gè)
cat
攘须,先調(diào)用remapClass(cat->cls)
漆撞,并返回一個(gè)objc_class *
對象cls
。這一步的目的在于找到到category對應(yīng)的類對象cls
于宙。 - 找到category對應(yīng)的類對象
cls
后浮驳,就開始進(jìn)行對cls的修改操作了。首先捞魁,如果category中有實(shí)例方法至会,協(xié)議,和實(shí)例屬性之一的話谱俭,則直接對cls進(jìn)行操作奉件。如果category中包含了類方法,協(xié)議昆著,類屬性(不支持)之一的話县貌,還要對cls
所對應(yīng)的元類(cls->ISA())
進(jìn)行操作。 - 不管是對cls還是cls的元類進(jìn)行操作宣吱,都是調(diào)用的方法
addUnattachedCategoryForClass
窃这。但這個(gè)方法并不是category實(shí)現(xiàn)的關(guān)鍵,其內(nèi)部邏輯只是將class和其對應(yīng)的category做了一個(gè)映射征候。這樣杭攻,以class為key,就可以取到所其對應(yīng)的所有的category疤坝。 - 做好class和category的映射后兆解,會(huì)調(diào)用
remethodizeClass
方法來修改class的method list結(jié)構(gòu),這才是runtime實(shí)現(xiàn)category的關(guān)鍵所在跑揉。
remethodizeClass
既然remethodizeClass
是category的實(shí)現(xiàn)核心锅睛,那么我們就單獨(dú)一節(jié)埠巨,細(xì)看一下該方法的實(shí)現(xiàn):
/***********************************************************************
* remethodizeClass
* Attach outstanding categories to an existing class.
* Fixes up cls's method list, protocol list, and property list.
* Updates method caches for cls and its subclasses.
* Locking: runtimeLock must be held by the caller
**********************************************************************/
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);
}
}
該段代碼首先通過unattachedCategoriesForClass
取出還未被附加到class上的category list,然后調(diào)用attachCategories
將這些category附加到class上现拒。
attachCategories
的實(shí)現(xiàn)如下:
// Attach method lists and properties and protocols from categories to a class.
// Assumes the categories in cats are all loaded and sorted by load order,
// oldest categories first.
static void
attachCategories(Class cls, category_list *cats, bool flush_caches)
{
if (!cats) return;
if (PrintReplacedMethods) printReplacements(cls, cats);
bool isMeta = cls->isMetaClass();
// 首先分配method_list_t *辣垒, property_list_t *, protocol_list_t *的數(shù)組空間印蔬,數(shù)組大小等于category的個(gè)數(shù)
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--) { // 依次讀取每一個(gè)category勋桶,將其methods,property侥猬,protocol添加到mlists例驹,proplist,protolist中存儲(chǔ)
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;
}
}
// 取出class的data()數(shù)據(jù)退唠,其實(shí)是class_rw_t * 指針鹃锈,其對應(yīng)結(jié)構(gòu)體實(shí)例存儲(chǔ)了class的基本信息
auto rw = cls->data();
prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
rw->methods.attachLists(mlists, mcount); // 將category中的method 添加到class中
free(mlists);
if (flush_caches && mcount > 0) flushCaches(cls); // 如果需要,同時(shí)刷新class的method list cache
rw->properties.attachLists(proplists, propcount); // 將category的property添加到class中
free(proplists);
rw->protocols.attachLists(protolists, protocount); // 將category的protocol添加到class中
free(protolists);
}
到此為止瞧预,我們就完成了category的加載工作屎债。可以看到松蒜,最終扔茅,cateogry被加入到了對應(yīng)class的方法,協(xié)議以及屬性列表中秸苗。
最后我們再看一下attachLists
方法是如何將兩個(gè)list合二為一的:
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]));
}
}
仔細(xì)看會(huì)發(fā)現(xiàn)召娜,attachLists
方法其實(shí)是使用的‘頭插
’的方式將新的list插入原有l(wèi)ist中的。即惊楼,新的list會(huì)插入到原始list的頭部玖瘸。
<font color=orange>這也就說明了,為什么category中的方法檀咙,會(huì)‘覆蓋’class的原始方法雅倒。其實(shí)并沒有真正的‘覆蓋’,而是由于cateogry中的方法被排到了原始方法的前面弧可,那么在消息查找流程中蔑匣,會(huì)返回首先被查找到的cateogry方法的實(shí)現(xiàn)。</font>
category和+load方法
在面試時(shí)棕诵,可能被問到這樣的問題:
在類的+load方法中裁良,可以調(diào)用分類方法嗎?
要回答這個(gè)問題校套,其實(shí)要搞清load方法的調(diào)用時(shí)機(jī)和category附加到class上的先后順序价脾。
如果在load方法被調(diào)用前,category已經(jīng)完成了附加到class上的流程笛匙,則對于上面的問題侨把,答案是肯定的犀变。
我們回到runtime的入口函數(shù)來看一下,
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);
}
runtime在入口點(diǎn)分別向dyld注冊了三個(gè)事件監(jiān)聽:mapped oc sections秋柄, init oc section 以及 unmapped oc sections获枝。
而這三個(gè)事件的順序是: mapped oc sections
-> init oc section
-> unmapped oc sections
在mapped oc sections
事件中,我們已經(jīng)看過其源碼骇笔,runtime會(huì)依次讀取Mach-O文件中的oc sections映琳,并根據(jù)這些信息來初始化runtime環(huán)境。這其中就包括cateogry的加載蜘拉。
之后,當(dāng)runtime環(huán)境都初始化完畢有鹿,在dyld的init oc section
事件中旭旭,runtime會(huì)調(diào)用每一個(gè)加載到內(nèi)存中的類的+load
方法。
這里我們注意到葱跋,+load
方法的調(diào)用是在cateogry加載之后的持寄。因此,在+load
方法中娱俺,是可以調(diào)用category方法的稍味。
調(diào)用已被category‘覆蓋’的方法
前面我們已經(jīng)知道,類中的方法并不是真正的被category‘覆蓋’荠卷,而是被放到了類方法列表的后面模庐,消息查找時(shí)找不到而已。我們當(dāng)然也可以手動(dòng)來找到并調(diào)用它油宜,代碼如下:
@interface Son : NSObject
- (void)sayHi;
@end
@implementation Son
- (void)sayHi {
NSLog(@"Son say hi!");
}
@end
// son 的分類掂碱,覆寫了sayHi方法
@interface Son (Good)
- (void)sayHi;
- (void)saySonHi;
@end
- (void)sayHi {
NSLog(@"Son's category good say hi");
}
- (void)saySonHi {
unsigned int methodCount = 0;
Method *methodList = class_copyMethodList([self class], &methodCount);
SEL sel = @selector(sayHi);
NSString *originalSelName = NSStringFromSelector(sel);
IMP lastIMP = nil;
for (NSInteger i = 0; i < methodCount; ++i) {
Method method = methodList[I];
NSString *selName = NSStringFromSelector(method_getName(method));
if ([originalSelName isEqualToString:selName]) {
lastIMP = method_getImplementation(method);
}
}
if (lastIMP != nil) {
typedef void(*fn)(id, SEL);
fn f = (fn)lastIMP;
f(self, sel);
}
free(methodList);
}
// 分別調(diào)用sayHi 和 saySonHi
Son *mySon1 = [Son new];
[mySon1 sayHi];
[mySon1 saySonHi];
輸出為:
果然,我們調(diào)用到了原始的sayHi
方法慎冤。
category和關(guān)聯(lián)對象
眾所周知疼燥,category是不支持向類添加實(shí)例變量的。這在源碼中也可以看出蚁堤,cateogry僅支持實(shí)例方法醉者、類方法、協(xié)議披诗、和實(shí)例屬性(注意撬即,實(shí)例屬性并不等于實(shí)例變量)。
但是藤巢,runtime也給我提供了一個(gè)折中的方式搞莺,雖然不能夠向類添加實(shí)例變量,但是runtime為我們提供了方法掂咒,可以向類的實(shí)例對象添加關(guān)聯(lián)對象才沧。
所謂關(guān)聯(lián)對象迈喉,就是為目標(biāo)對象添加一個(gè)關(guān)聯(lián)的對象,并能夠通過key來查找到這個(gè)關(guān)聯(lián)對象温圆。說的形象一點(diǎn)挨摸,就像我們?nèi)ヌ瑁瑀untime可以給我們分配一個(gè)舞伴一樣岁歉。
這種關(guān)聯(lián)是對象和對象級(jí)別的得运,而不是類層次上的。當(dāng)你為一個(gè)類實(shí)例添加一個(gè)關(guān)聯(lián)對象后锅移,如果你再創(chuàng)建另一個(gè)類實(shí)例熔掺,這個(gè)新建的實(shí)例是沒有關(guān)聯(lián)對象的。
我們可以通過重寫set/get方法的形式非剃,來自動(dòng)為我們的實(shí)例添加關(guān)聯(lián)對象置逻。
MyClass+Category1.h:
#import "MyClass.h"
@interface MyClass (Category1)
@property(nonatomic,copy) NSString *name;
@end
MyClass+Category1.m:
#import "MyClass+Category1.h"
#import <objc/runtime.h>
@implementation MyClass (Category1)
+ (void)load
{
NSLog(@"%@",@"load in Category1");
}
- (void)setName:(NSString *)name
{
objc_setAssociatedObject(self,
"name",
name,
OBJC_ASSOCIATION_COPY);
}
- (NSString*)name
{
NSString *nameObject = objc_getAssociatedObject(self, "name");
return nameObject;
}
@end
代碼很簡單,我們重點(diǎn)關(guān)注一下其背后的實(shí)現(xiàn)备绽。
objc_setAssociatedObject
我們要設(shè)置關(guān)聯(lián)對象券坞,需要調(diào)用objc_setAssociatedObject
方法將對象關(guān)聯(lián)到目標(biāo)對象上。我們需要傳入4個(gè)參數(shù):target object肺素, associated key恨锚, associated value, objc_AssociationPolicy。
objc_AssociationPolicy是一個(gè)枚舉倍靡,可以取值為:
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
* The association is not made atomically. */
OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
* The association is not made atomically. */
OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
* The association is made atomically. */
OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
* The association is made atomically. */
};
分別和property的屬性定義一一匹配猴伶。
當(dāng)我們?yōu)閷ο笤O(shè)置關(guān)聯(lián)對象的時(shí)候,所關(guān)聯(lián)的對象到底存在了那里呢塌西?我們看源碼:
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) {
_object_set_associative_reference(object, (void *)key, value, policy);
}
void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
// retain the new value (if any) outside the lock.
ObjcAssociation old_association(0, nil);
id new_value = value ? acquireValue(value, policy) : nil;
{
AssociationsManager manager; // 這是一個(gè)單例蜗顽,內(nèi)部保存一個(gè)全局的static AssociationsHashMap *_map; 用于保存所有的關(guān)聯(lián)對象。
AssociationsHashMap &associations(manager.associations());
disguised_ptr_t disguised_object = DISGUISE(object); // 取反object 地址 作為accociative key
if (new_value) {
// break any existing association.
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// secondary table exists
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
j->second = ObjcAssociation(policy, new_value);
} else {
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
// create the new association (first time).
ObjectAssociationMap *refs = new ObjectAssociationMap;
associations[disguised_object] = refs;
(*refs)[key] = ObjcAssociation(policy, new_value);
object->setHasAssociatedObjects(); // 將object標(biāo)記為 has AssociatedObjects
}
} else { // 如果傳入的關(guān)聯(lián)對象值為nil雨让,則斷開關(guān)聯(lián)
// setting the association to nil breaks the association.
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
refs->erase(j);
}
}
}
}
// release the old value (outside of the lock).
if (old_association.hasValue()) ReleaseValue()(old_association); // 釋放掉old關(guān)聯(lián)對象雇盖。(如果多次設(shè)置同一個(gè)key的value,這里會(huì)釋放之前的value)
}
大體流程為:
- 根據(jù)關(guān)聯(lián)的policy,調(diào)用
id new_value = value ? acquireValue(value, policy) : nil;
栖忠,acquireValue
方法會(huì)根據(jù)poilcy是retain或copy崔挖,對value做引用+1
操作或copy
操作,并返回對應(yīng)的new_value
庵寞。(如果傳入的value為nil
狸相,則返回nil
,不做任何操作)
acquireValue
實(shí)現(xiàn)代碼是:
static id acquireValue(id value, uintptr_t policy) {
switch (policy & 0xFF) {
case OBJC_ASSOCIATION_SETTER_RETAIN:
return objc_retain(value);
case OBJC_ASSOCIATION_SETTER_COPY:
return ((id(*)(id, SEL))objc_msgSend)(value, SEL_copy);
}
return value;
}
- 獲取到
new_value
后捐川,根據(jù)是否有new_value
的值脓鹃,進(jìn)入不同流程。如果new_value
存在古沥,則對象與目標(biāo)對象關(guān)聯(lián)瘸右。實(shí)質(zhì)是存入到全局單例AssociationsManager manager
的對象關(guān)聯(lián)表中娇跟。 如果new_value
不存在,則釋放掉之前目標(biāo)對象及關(guān)聯(lián) key所存儲(chǔ)的關(guān)聯(lián)對象太颤。實(shí)質(zhì)是在AssociationsManager
中刪除掉關(guān)聯(lián)對象苞俘。 - 最后,釋放掉之前以同樣key存儲(chǔ)的關(guān)聯(lián)對象龄章。
其中吃谣,起到關(guān)鍵作用的在于AssociationsManager manager
癌蚁, 它是一個(gè)全局單例驴党,其成員變量為static AssociationsHashMap *_map
钟哥,用于存儲(chǔ)目標(biāo)對象及其關(guān)聯(lián)的對象肤视。_map
中的數(shù)據(jù)存儲(chǔ)結(jié)構(gòu)如下圖所示:
仔細(xì)看這一段代碼,會(huì)發(fā)現(xiàn)有個(gè)問題:當(dāng)我們第一次為目標(biāo)對象創(chuàng)建關(guān)聯(lián)對象時(shí)塞弊,會(huì)在AssociationsManager manager
的ObjectAssociationMap
中插入一個(gè)以disguised_object
為key 的節(jié)點(diǎn)菠发,用于存儲(chǔ)該目標(biāo)對象所關(guān)聯(lián)的對象艺蝴。
但是惋鸥,上面代碼中,僅有釋放old_association
關(guān)聯(lián)對象的代碼悍缠,而沒有釋放保存在AssociationsManager manager
中節(jié)點(diǎn)的代碼卦绣。那么,AssociationsManager manager
中的節(jié)點(diǎn)是什么時(shí)候被釋放的呢飞蚓?
在對象的銷毀邏輯里滤港,會(huì)調(diào)用objc_destructInstance
,實(shí)現(xiàn)如下:
void *objc_destructInstance(id obj)
{
if (obj) {
// Read all of the flags at once for performance.
bool cxx = obj->hasCxxDtor();
bool assoc = obj->hasAssociatedObjects();
// This order is important.
if (cxx) object_cxxDestruct(obj); // 調(diào)用C++析構(gòu)函數(shù)
if (assoc) _object_remove_assocations(obj); // 移除所有的關(guān)聯(lián)對象趴拧,并將其自身從AssociationsManager的map中移除
obj->clearDeallocating(); // 清理ARC ivar
}
return obj;
}
obj
的關(guān)聯(lián)對象會(huì)在_object_remove_assocations
方法中全部移除溅漾,同時(shí),會(huì)將obj
自身從AssociationsManager
的map
中移除:
void _object_remove_assocations(id object) {
vector< ObjcAssociation,ObjcAllocator<ObjcAssociation> > elements;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
if (associations.size() == 0) return;
disguised_ptr_t disguised_object = DISGUISE(object);
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// copy all of the associations that need to be removed.
ObjectAssociationMap *refs = i->second;
for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) {
elements.push_back(j->second);
}
// remove the secondary table.
delete refs;
associations.erase(i);
}
}
// the calls to releaseValue() happen outside of the lock.
for_each(elements.begin(), elements.end(), ReleaseValue());
}