objective c底層 runtime機制

2022-07-04 00:12:12 字數 1155 閱讀 2503

runtime是oc的真面目。oc底層的一套c語言api.

unsigned int

count;

//獲取屬性列表

objc_property_t *propertylist = class_copypropertylist([self class], &count);

for (unsigned int i=0; i)

//獲取方法列表

method *methodlist = class_copymethodlist([self class], &count);

for (unsigned int i; i)

//獲取成員變數列表

ivar *ivarlist = class_copyivarlist([self class], &count);

for (unsigned int i; i)

//獲取協議列表

__unsafe_unretained protocol **protocollist = class_copyprotocollist([self class], &count);

for (unsigned int i; i)

應用1:kvc字典轉模型

class clazz = person.class

;unsigned

int count = 0

;person *person =[[person alloc]init];

nsdictionary *dict = @;

ivar *ivars = class_copyivarlist(clazz, &count);

for (int i = 0; i < count; i++)

[person setvalue:value forkey:key];

}nslog(

@"%@

", person);

應用2:nscoding歸檔和解檔

- (void)encodewithcoder:(nscoder *)acoder 

}- (id)initwithcoder:(nscoder *)adecoder

}return

self;

}

其他應用場景:

Objective C 分類底層原理分析

ios分類使用的場景就是把功能模組化,把一組類似的功能擴充套件出乙個分類,便於 的維護以及功能的分塊 分類在ios底層的資料結構如下 struct category t name 被分類的類名稱 cls isa指標 instance methods 物件方法列表 class methods 類方法列...

run命令總結

授權管理器 azman.msc xml證書 certmgr.msc 元件服務 compex.msc 計算機管理 compmgmt.msc 裝置管理器 devmgmt.msc 磁碟管理 diskmgmt.msc 事件檢視器 eventvwr.msc 共享資料夾 fsmgmt.msc 本地組策略編輯器 ...

控制由Run執行的程式 簡稱Run程式

在pb程式設計中,可以用run 來執行一些程式。比如使用者按了f1,就執行乙個chm檔案。但run程式無法 與pb主程式協調工作,若使用者按了多次f1,就會啟動run程式的多個例項,主程式退出時,run程式依然 執行。可以用如下函式來使它們協調工作 在外部函式中引用 function ulong f...