前言
前幾天有人問我一個問題:為什么分類不能自動創(chuàng)建get set方法。老實說,筆者從來沒有去思考過這個問題。于是這次通過代碼實踐跟runtime
源碼來探究這個問題。
準備工作
為了能減少輸出類數(shù)據(jù)的代碼工作停巷,筆者基于NSObject
的分類封裝了一套代碼
其中輸出類實例變量的具體代碼:
- (void)logIvarsWithExpReg: (NSString *)expReg customed: (BOOL)customed {
[NSObject kRecordOBJ];
unsigned int ivarCount;
Ivar * ivars = class_copyIvarList([self class], &ivarCount);
for (int idx = 0; idx < ivarCount; idx++) {
Ivar ivar = ivars[idx];
NSString * ivarName = [NSString stringWithUTF8String: ivar_getName(ivar)];
if (customed && [kOBJIvarNames containsObject: ivarName]) {
continue;
}
if (expReg && !kValidExpReg(ivarName, expReg)) {
continue;
}
printf("ivar: %s --- %s\n", NSStringFromClass([self class]).UTF8String, ivarName.UTF8String);
}
free(ivars);
}
+(void)kRecordOBJ
采用dispatch_once
的方式將NSObject
存在的數(shù)據(jù)存儲到三個數(shù)組中,用來排除父類的數(shù)據(jù)輸出
類的屬性
-
正常創(chuàng)建類
@interface Person: NSObject {
int _pId;
}@property (nonatomic, copy) NSString * name; @property (nonatomic, assign) NSUInteger age; @end int main(int argc, char * argv[]) { @autoreleasepool { Person * p = [[Person alloc] init]; [p logCustomIvars]; [p logCustomMethods]; [p logCustomProperties]; return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
運行結(jié)果:屬性name
和age
生成了對應(yīng)的_propertyName
的實例變量以及setter
和getter
-
動態(tài)生成屬性
age
@implementation Person
@dynamic age;@end
運行結(jié)果:缺少了_age
變量以及對應(yīng)的setAge:
和age
方法
-
手動實現(xiàn)
setter/getter
@implemetation Person
@dynamic age;- (void)setAge: (NSUInteger)age {} - (NSUInteger)age { return 18; } @end
輸出結(jié)果:未生成_age
實例變量
-
手動實現(xiàn)
_pId
的setter/getter
@implemetation Person
@dynamic age;- (void)setAge: (NSUInteger)age {} - (NSUInteger)age { return 18; } - (void)setPId: (int)pId { _pId = pId; } - (int)pId { return _pId; } @end [p setValueForKey: @"pId"];
運行結(jié)果:KVC
的訪問會觸發(fā)setter
方法榕栏,_pId
除了無法通過點語法訪問外畔勤,其他表現(xiàn)與@property
無異
通過上面的幾段試驗,可以得出@property
的公式:
分類屬性
-
分類中添加
weigh
和height
屬性
@interface Person (category)@property (nonatomic, assign) CGFloat weigh; @property (nonatomic, assign) CGFloat height; @end
運行結(jié)果:weigh
和height
未生成實例變量以及對應(yīng)的setter/getter
扒磁,與@dynamic
修飾的age
表現(xiàn)一致
-
使用
@synthesize
自動合成setter/getter
方法時編譯報錯
-
手動實現(xiàn)
setter/getter
@implemetation Person (category)- (void)setWeigh: (CGFloat)weigh {} - (CGFloat)weigh { return 150; } @end
運行結(jié)果:與@dynamic age
后重寫其setter/getter
表現(xiàn)一致
-
動態(tài)綁定屬性來實現(xiàn)
setter/getter
void * kHeightKey = &kHeightKey;
@implemetation Person (category)- (void)setWeigh: (CGFloat)weigh {} - (CGFloat)weigh { return 150; } - (void)setHeight: (CGFloat)height { objc_setAssociatedObject(self, kHeightKey, @(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (CGFloat)height { return return [objc_getAssociatedObject(self, kHeightKey) doubleValue];; } @end [p logCustomIvars] [p logCustomMethods]; [p logCustomProperties]; CGFloat height = 180; p.height = 180; height = p.height; [p logCustomIvars] [p logCustomMethods]; [p logCustomProperties];
運行結(jié)果:動態(tài)綁定前后ivar
沒有發(fā)生任何變化
通過代碼實驗庆揪,可以得出下面兩個結(jié)論:
- 分類屬性相當于
@dynamic property
- 缺少
ivar
的情況下無法使用@synthesize
自動合成屬性
以及一個猜想:
- 在類完成加載后無法繼續(xù)添加
ivar
通過runtime動態(tài)創(chuàng)建類驗證猜想:
int main(int argc, char * argv[]) {
NSString * className = @"Custom";
Class customClass = objc_allocateClassPair([NSObject class], className.UTF8String, 0);
class_addIvar(customClass, @"ivar1".UTF8String, sizeof(NSString *), 0, "@");
objc_property_attribute_t type1 = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership1 = { "C", "N" };
objc_property_attribute_t atts1[] = { type1, ownership1 };
class_addProperty(customClass, "property1", atts1, 2);
objc_registerClassPair(customClass);
id instance = [[customClass alloc] init];
NSLog(@"\nLog Ivars ===================");
[instance logCustomIvars];
NSLog(@"\nLog methods ===================");
[instance logCustomMethods];
NSLog(@"\nLog properties ===================");
[instance logCustomProperties];
class_addIvar(customClass, @"ivar2".UTF8String, sizeof(NSString *), 0, "@");
objc_property_attribute_t type2 = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership2 = { "C", "N" };
objc_property_attribute_t atts2[] = { type2, ownership2 };
class_addProperty(customClass, "property2", atts2, 2);
instance = [[customClass alloc] init];
NSLog(@"\nLog Ivars ===================");
[instance logCustomIvars];
NSLog(@"\nLog methods ===================");
[instance logCustomMethods];
NSLog(@"\nLog properties ===================");
[instance logCustomProperties];
}
運行結(jié)果:在調(diào)用class_registerClassPair
后,添加ivar
失敗
從源碼解析
objc_class
的結(jié)構(gòu)體定義如下:
struct objc_class : objc_object {
Class superclass;
const char *name;
uint32_t version;
uint32_t info;
uint32_t instance_size;
struct old_ivar_list *ivars;
struct old_method_list **methodLists;
Cache cache;
struct old_protocol_list *protocols;
// CLS_EXT only
const uint8_t *ivar_layout;
struct old_class_ext *ext;
}
ps: 在新版本中結(jié)構(gòu)體內(nèi)部已經(jīng)發(fā)生了大改妨托,但是內(nèi)部的屬性大致上仍是這些
這里面有個重要的屬性ivar_layout
缸榛,顧名思義存放的是變量的位置屬性,與之對應(yīng)的還有一個weakIvarLayout
變量兰伤,不過在默認結(jié)構(gòu)中沒有出現(xiàn)内颗。這兩個屬性用來記錄ivar
哪些是strong
或者weak
,而這個記錄操作在runtime
階段已經(jīng)被確定好敦腔。正由于如此均澳,這極有可能是ivar
無法在類被加載后繼續(xù)添加的原因之一。ivar_layout
的更多了解可以參照Objective-C Class Ivar layout一文
import
操作幫助編譯檢查和鏈接過程符衔,但是在category
的加載過程中找前,不會將擴展的內(nèi)容添加到原始的類結(jié)構(gòu)中。runtime
對于category
的加載過程可以簡單的分成下面幾步(摘自objc category的密碼):
-
objc runtime
的加載入口是一個叫_objc_init
的方法判族,在library
加載前由libSystem dyld
調(diào)用躺盛,進行初始化操作 - 調(diào)用
map_images
方法將文件中的image
map
到內(nèi)存 - 調(diào)用
_read_images
方法初始化map
后的image
,這里面干了很多的事情五嫂,像load
所有的類、協(xié)議和category
,著名的+ load
方法就是這一步調(diào)用的
-仔細看category
的初始化沃缘,循環(huán)調(diào)用了_getObjc2CategoryList
方法躯枢,這個方法拿出來看看: - .…
這一切的過程發(fā)生在_objc_init
函數(shù)中,函數(shù)實現(xiàn)如下
簡單來說在
load_images
函數(shù)中最終會走到下面的代碼調(diào)用來加載所有的類以及類的分類根據(jù)上面的代碼加上
runtime
的加載順序槐臀,可以繼續(xù)推出:
-
@dynamic
實際上是將屬性的加載推遲到類加載完成后
另外锄蹂,前面也說過在缺少ivar
的情況下無法自動合成setter/getter
,除了category
本身是不被添加到類結(jié)構(gòu)中的水慨,所以無法使用類結(jié)構(gòu)的ivar
合成屬性外得糜,還有分類自身結(jié)構(gòu)的問題
struct category_t {
const char *name; /// 類名
classref_t cls; /// 類指針
struct method_list_t *instanceMethods; /// 實例方法
struct method_list_t *classMethods; /// 類方法
struct protocol_list_t *protocols; /// 擴展的協(xié)議
struct property_list_t *instanceProperties; /// 擴展屬性
method_list_t *methodsForMeta(bool isMeta) { ... }
property_list_t *propertiesForMeta(bool isMeta) { ... }
};
可以看到分類結(jié)構(gòu)本身是不存在ivar
的容器的,因此缺少了自動合成屬性的條件晰洒。最后還有一個問題朝抖,我們在使用objc_associate
系列函數(shù)綁定屬性的時候這些變量存儲在了哪里?
總結(jié)
首先谍珊,iOS的分類在runtime
實現(xiàn)的結(jié)構(gòu)體中并不存在Ivar
類型的容器治宣,缺少了自動合成setter
以及getter
的必要條件,因此在分類中聲明的屬性默認為@dynamic
修飾砌滞。
其次侮邀,OC本身是一門原型語言,對象和類原型很像贝润。類對象執(zhí)行alloc
方法就像是原型模式中的copy
操作一樣绊茧,類保存了copy
所需的實例信息,這些信息內(nèi)存信息在runtime
加載時就被固定了打掘,沒有擴充Ivar
的條件华畏。(感謝大表哥的科普)
最后,在runtime
中存在一個類型為AssociationHashMap
的哈希映射表保存著對象動態(tài)添加的屬性胧卤,每個對象以自身地址為key
維護著一個綁定屬性表唯绍,我們動態(tài)添加的屬性就都存儲在這個表里,這也是動態(tài)添加property
能成功的基礎(chǔ)枝誊。
上一篇:閑聊內(nèi)存管理
轉(zhuǎn)載請注明原文地址及作者