OC是運行時語言,只有在程序運行時,才會去確定對象的類型,并調用類與對象相應的方法。平時編寫的OC代碼, 在程序運行過程中, 其實最終都是轉成了runtime的C語言代碼, runtime算是OC的幕后工作者。利用runtime機制讓我們可以在程序運行時動態修改類、對象中的所有屬性、方法,就算是私有方法以及私有屬性都是可以動態修改的。
runtime是屬于OC的底層, 可以進行一些非常底層的操作。在程序運行過程中, 動態創建一個類, 動態地為某個類添加屬性\方法, 修改屬性值\方法,遍歷一個類的所有成員變量(屬性)\所有方法等。
相關知識點
Ivar:定義對象的實例變量,包括類型和名字。
objc_property_t:定義屬性。這個名字可能是為了防止和Objective-C 1.0中的用戶類型沖突,那時候還沒有屬性。
Method:成員方法。這個類型提供了方法的名字(就是選擇器)、參數數量和類型,以及返回值(這些信息合起來稱為方法的簽名),還有一個指向代碼的函數指針(也就是方法的實現)。
SEL:定義選擇器。選擇器是方法名的唯一標識符,我理解它就是個字符串。
簡單代碼實現
1.示例結構
首先定義了一個繼承NSObject的測試類RC,里面是一些簡單的屬性,方法。
.h文件
#import <Foundation/Foundation.h>
@interface RC : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *intro;
@property (nonatomic,copy) NSString *name;
- (instancetype)initWithDic:(NSDictionary *)dic;
+ (instancetype)testWithDic:(NSDictionary *)dic;
+ (NSArray *)testsList;
@end
.m文件
#import "RC.h"
@implementation RC
- (instancetype)initWithDic:(NSDictionary *)dic
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
+ (instancetype)testWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
+ (NSArray *)testsList
{
//加載plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
//字典轉模型
NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *dic in dicArray) {
RC *test = [RC testWithDic:dic];
[tmpArray addObject:test];
}
return tmpArray;
}
@end
從storyboard里拖一個按鈕,關聯到viewController里。
viewController.m文件簡單代碼:
#import "ViewController.h"
#import <objc/runtime.h>
#import "RC.h"
@interface ViewController ()
- (IBAction)btn:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)btn:(UIButton *)sender {
[[self class] printMethodList];
}
+ (void)printIvarList
{
u_int count;
Ivar *ivarlist = class_copyIvarList([RC class], &count);
for (int i = 0; i < count; i++)
{
const char *ivarName = ivar_getName(ivarlist[i]);
NSString *strName = [NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding];
NSLog(@"ivarName:%@", strName);
}
}
+ (void)printPropertyList
{
u_int count;
objc_property_t *properties = class_copyPropertyList([RC class], &count);
for (int i = 0; i < count; i++)
{
const char *propertyName = property_getName(properties[i]);
NSString *strName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];
NSLog(@"propertyName:%@", strName);
}
}
+ (void)printMethodList
{
u_int count;
Method *methods = class_copyMethodList([RC class], &count);
for (int i = 0; i < count; i++)
{
SEL methodName = method_getName(methods[i]);
NSString *strName = [NSString stringWithCString:sel_getName(methodName) encoding:NSUTF8StringEncoding];
NSLog(@"methodName:%@", strName);
}
}
@end
因為代碼很簡單,就不上傳項目了。大家可以看到,上面最主要的就是取變量,屬性和方法的三個方法:printIvarList,printPropertyList,printMethodList。分別執行每個方法,便能得到RC這個測試類的相關結果。
下面,我們測試下:
執行printIvarList方法,我們會得到下面的結果。
執行printPropertyList方法,我們會得到下面的結果。
執行printMethodList方法,我們會得到下面的結果。
看完上面的結果,以前不太明了的變量和屬性的區別是不是一目了然了。
程序中self.name其實是調用的name屬性的getter/setter方法,_name是實例變量。在oc中點表達式其實就是調用對象的setter和getter方法的一種快捷方式。
在ios第一版中,我們為輸出需要同時聲明了屬性和底層實例變量,那時,屬性是oc語言的一個新的機制,并且要求你必須聲明與之對應的實例變量。蘋果將默認編譯器從GCC轉換為LLVM(low level virtual machine),從此不再需要為屬性聲明實例變量了。如果LLVM發現一個沒有匹配實例變量的屬性,它將自動創建一個以下劃線開頭的實例變量。因此,我們不再為輸出而聲明實例變量。
在方法名的結果中我們可以看到,打印出的方法有三個屬性的getter/setter方法和一個實例方法,class_copyMethodList只能得到成員方法的。但是有個奇怪的方法<code>methodName:.cxx_destruct</code>,這個是什么呢?
.cxx_destruct方法
通過apple的runtime源碼,不難發現NSObject執行dealloc時調用_objc_rootDealloc繼而調用object_dispose隨后調objc_destructInstance方法,前幾步都是條件判斷和簡單的跳轉,最后的這個函數如下:
void *objc_destructInstance(id obj)
{
if (obj) {
Class isa_gen = _object_getClass(obj);
class_t *isa = newcls(isa_gen);
// Read all of the flags at once for performance.
bool cxx = hasCxxStructors(isa);
bool assoc = !UseGC && _class_instancesHaveAssociatedObjects(isa_gen);
// This order is important.
if (cxx) object_cxxDestruct(obj);
if (assoc) _object_remove_assocations(obj);
if (!UseGC) objc_clear_deallocating(obj);
}
return obj;
}
簡單明確的干了三件事:
1.執行一個叫object_cxxDestruct自動釋放實例變量
2.執行_object_remove_assocations去除和這個對象assocate的對象(常用于category中添加帶變量的屬性,這也是為什么ARC下沒必要remove一遍的原因)
3.執行objc_clear_deallocating,清空引用計數表并清除弱引用表,將所有weak引用指nil(這也就是weak變量能安全置空的所在)
名為object_cxxDestruct的方法最終成為下面的調用:
static void object_cxxDestructFromClass(id obj, Class cls)
{
void (*dtor)(id);
// Call cls's dtor first, then superclasses's dtors.
for ( ; cls != NULL; cls = _class_getSuperclass(cls)) {
if (!_class_hasCxxStructors(cls)) return;
dtor = (void(*)(id))
lookupMethodInClassAndLoadCache(cls, SEL_cxx_destruct);
if (dtor != (void(*)(id))_objc_msgForward_internal) {
if (PrintCxxCtors) {
_objc_inform("CXX: calling C++ destructors for class %s",
_class_getName(cls));
}
(*dtor)(obj);
}
}
}
沿著繼承鏈逐層向上搜尋SEL_cxx_destruct這個selector,找到函數實現(void (*)(id)(函數指針)并執行。搜索這個selector的聲明,發現是名為.cxx_destruct的方法,以點開頭的名字,和unix的文件一樣,是有隱藏屬性的。
.cxx_destruct方法原本是為了C++對象析構的,ARC借用了這個方法插入代碼實現了自動內存釋放的工作。
經過幾次試驗,發現:
1.只有在ARC下這個方法才會出現
2.只有當前類擁有實例變量時(不論是不是用property)這個方法才會出現,且父類的實例變量不會導致子類擁有這個方法
3.出現這個方法和變量是否被賦值,賦值成什么沒有關系
結論
.cxx_destruct是編譯器生成的代碼,在.cxx_destruct進行形如objc_storeStrong(&ivar, null)的調用后,這個實例變量就被release和設置成nil了。ARC下對象的成員變量于編譯器插入的.cxx_desctruct方法自動釋放。
更多有趣好玩的東西,我們下次再聊。