iOS開發(fā)中Runtime.h / objc.h / message.h比較重要的聲明

methodForSelector  //返回指定方法實現(xiàn)的地址

performSelector: withObject  //執(zhí)行SEL所指代的方法

NSClassFromString;

NSSelectorFromString;

performSelector:

// 動態(tài)方法處理--提供再一次實現(xiàn)方法的機會. 在沒有真正提供方法實現(xiàn)的,并提供了消息轉(zhuǎn)發(fā)機制的情況下,return YES 表示不進行后續(xù)的消息轉(zhuǎn)發(fā),NO表示要進行后續(xù)的消息轉(zhuǎn)發(fā).

+ (BOOL)resolveClassMethod:(SEL)name;

+ (BOOL)resolveInstanceMethod:(SEL)name;

// 在類和cache緩存中查找方法的地址

static IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel);

// 查找方法

static Method look_up_method(Class cls, SEL sel, BOOL withCache, BOOL withResolver);

forwardInvocation: // 不能識別的消息分發(fā)中心----在方法的實現(xiàn)中將消息轉(zhuǎn)發(fā)個其他對象去實現(xiàn).

invokeWithTarget: // 轉(zhuǎn)發(fā)的消息通過這個函數(shù)來實現(xiàn)方法轉(zhuǎn)發(fā).

/* class類 / 使用Class代替struct objc_class * */

typedef struct objc_class *Class;

typedef struct objc_object {

  Class isa;

} *id;

struct objc_class {

  Class isa;  //指向父類或者元類

  Class super_class  ;    //父類

  const char *name  ;    //類名

  long version  ;    //版本

  long info  ;    //信息

  long instance_size  ;    //實例變量的大小

  struct objc_ivar_list *ivars  ;    //成員變量列表

  struct objc_method_list **methodLists  ;    //方法列表,存儲的是Method類型的方法

  struct objc_cache *cache  ;    //調(diào)用過得方法的緩存,提高下次執(zhí)行的效率.

  struct objc_protocol_list *protocols  ;    //要遵守的協(xié)議列表

} ;

/* Method方法結(jié)構(gòu)體*/

typedef struct objc_method *Method;

struct objc_method {

  SEL method_name ;    //方法名,也就是selector

  char *method_types ;    //方法的參數(shù)類型

  IMP method_imp ;    //函數(shù)指針,指向方法具體實現(xiàn)的指針..也即是selector的address

} ;

// SEL 和 IMP 配對是在運行時決定的.并且是一對一的.也就是通過selector去查詢IMP,找到執(zhí)行方法的地址,才能確定具體執(zhí)行的代碼.

// 消息選標SEL:selector / 實現(xiàn)地址IMP:address 在方法鏈表(字典)中是以key / value 形式存在的

typedef struct objc_selector    *SEL;  //方法的名稱--@selector(方法名)

typedef id (*IMP)(id, SEL, ...);    //函數(shù)指針I(yè)MP,指向方法的實現(xiàn)的指針  -----和block結(jié)構(gòu)一樣 void (^block)(int,int);

// IMP 函數(shù)指針,被指向的函數(shù)/方法,包含一個接收消息的對象id(self,指針),調(diào)用方法的選標SEL(方法名),以及...方法的個數(shù),并返回一個id.

// IMP是消息最終調(diào)用的代碼,是方法真正實現(xiàn)的代碼

/* 消息函數(shù) */ //編譯器會將消息轉(zhuǎn)換為對消息函數(shù)objc_msgSend的調(diào)用

id objc_msgSend(id self, SEL op, ...);

// id objc_msgSend(id theReceiver, SEL theSelector, ...);

// 三個參數(shù):消息接收者 id  方法名 SEL  參數(shù) ...

// [person run]; -- objc_msgSend(person, @selector(run));

struct objc_method_description {

  SEL name;  //方法名

  char *types;  //方法類型

};

struct objc_method_description_list {

  int count;

  struct objc_method_description list[1];

};

struct objc_method_list;  //方法列表

struct objc_method_list {

  struct objc_method_list *obsolete ;

  int method_count  ;

  int space ;

  struct objc_method method_list[1] ;

}  ;

/*成員變量*/

typedef struct objc_ivar *Ivar;

struct objc_ivar {

  char *ivar_name ;

  char *ivar_type ;

  int ivar_offset ;

  int space ;

} ;

struct objc_ivar_list {

  int ivar_count ;

  int space ;

  struct objc_ivar ivar_list[1] ;

}  ;

/*屬性的attribute*/

typedef struct objc_property *objc_property_t;

typedef struct {

  const char *name;

  const char *value;

} objc_property_attribute_t;

/*方法緩存結(jié)構(gòu)體*/

typedef struct objc_cache *Cache ;

struct objc_cache {

  unsigned int mask /* total = mask + 1 */ ;

  unsigned int occupied  ;

  Method buckets[1]  ;

};

/*分類*/

typedef struct objc_category *Category;

struct objc_category {

  char *category_name ;

  char *class_name ;

  struct objc_method_list *instance_methods ;

  struct objc_method_list *class_methods ;

  struct objc_protocol_list *protocols ;

} ;

/* 協(xié)議*/

typedef struct objc_object Protocol;

struct objc_protocol_list {

  struct objc_protocol_list *next;

  long count;

  Protocol *list[1];

};

/* Functions */

/*

object對象

*/

// 返回指定對象的一份拷貝

id object_copy(id obj, size_t size);

// 釋放指定對象占用的內(nèi)存

id object_dispose(id obj);

// 獲取實例對象的所屬的類

Class object_getClass(id obj) ;

// 設(shè)置實例對象的所屬的類

Class object_setClass(id obj, Class cls) ;

// 獲取實例對象的所屬類的類名

const char *object_getClassName(id obj);

// 返回指向給定對象分配的任何額外字節(jié)的指針

void *object_getIndexedIvars(id obj);

// 獲取實例對象的成員變量

id object_getIvar(id obj, Ivar ivar) ;

// 設(shè)置實例對象的成員變量

void object_setIvar(id obj, Ivar ivar, id value)

// 修改類實例的實例變量的值

Ivar object_setInstanceVariable(id obj, const char *name, void *value);

// 獲取對象實例變量的值

Ivar object_getInstanceVariable(id obj, const char *name, void **outValue);

// 返回指定類的元類

id objc_getMetaClass(const char *name);

// 返回指定類的類定義

id objc_lookUpClass(const char *name);

// 返回實例對象的類

id objc_getClass(const char *name);

id objc_getRequiredClass(const char *name);

Class objc_getFutureClass(const char *name) ;

void objc_setFutureClass(Class cls, const char *name);

// 獲取已注冊的類定義的列表

int objc_getClassList(Class *buffer, int bufferCount);

// 創(chuàng)建并返回一個指向所有已注冊類的指針列表

Class *objc_copyClassList(unsigned int *outCount);

Protocol *objc_getProtocol(const char *name);

Protocol * __unsafe_unretained *objc_copyProtocolList(unsigned int *outCount);

/*

class類

*/

// 獲取類的類名

const char *class_getName(Class cls) ;

// 是否是元類

BOOL class_isMetaClass(Class cls) ;

// 獲取類的父類

Class class_getSuperclass(Class cls) ;

// 設(shè)置新類的父類

Class class_setSuperclass(Class cls, Class newSuper) ;

// 類的版本信息

int class_getVersion(Class cls);

// 設(shè)置類的版本信息

void class_setVersion(Class cls, int version);

// 獲取該類實例對象大小

size_t class_getInstanceSize(Class cls) ;

// 獲取類中指定名稱實例對象的信息

Ivar class_getInstanceVariable(Class cls, const char *name);

// 獲取類成員變量的信息

Ivar class_getClassVariable(Class cls, const char *name) ;

// 獲取整個成員變量列表

Ivar *class_copyIvarList(Class cls, unsigned int *outCount) ;

// 獲取實例方法.

Method class_getInstanceMethod(Class cls, SEL name);

// 獲取類方法.

Method class_getClassMethod(Class cls, SEL name);

// 返回方法的具體實現(xiàn)

IMP class_getMethodImplementation(Class cls, SEL name) ;

// 返回方法的具體實現(xiàn)

IMP class_getMethodImplementation_stret(Class cls, SEL name) ;

// 檢查類是否響應(yīng)指定的消息.

BOOL class_respondsToSelector(Class cls, SEL sel) ;

// 獲取類方法列表.

Method *class_copyMethodList(Class cls, unsigned int *outCount);

// 檢查類是否實現(xiàn)了指定協(xié)議類的方法.

BOOL class_conformsToProtocol(Class cls, Protocol *protocol) ;

// 返回類遵守的協(xié)議列表.

Protocol * __unsafe_unretained *class_copyProtocolList(Class cls, unsigned int *outCount);

/*

object對象

*/

// 獲取指定的屬性

objc_property_t class_getProperty(Class cls, const char *name);

// 獲取屬性列表

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount);

// 創(chuàng)建實例對象

id class_createInstance(Class cls, size_t extraBytes);

// 在指定位置創(chuàng)建類實例

id objc_constructInstance(Class cls, void *bytes) ;

// 銷毀類實例

void *objc_destructInstance(id obj) ;

/*

動態(tài)創(chuàng)建類

*/

// 創(chuàng)建一個新類和元類  (ClassPair:包含類和元類)

Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes) ;

// 在應(yīng)用中注冊由objc_allocateClassPair創(chuàng)建的類

void objc_registerClassPair(Class cls) ;

// 復(fù)制一份類

Class objc_duplicateClass(Class original, const char *name,size_t extraBytes) ;

// 銷毀一個類及其相關(guān)聯(lián)的類

void objc_disposeClassPair(Class cls) ;

/*

class類

*/

//添加某個類的方法

BOOL class_addMethod(Class cls, SEL name, IMP imp,const char *types) ;

//替換某個類的方法

IMP class_replaceMethod(Class cls, SEL name, IMP imp,const char *types) ;

//添加某個類的變量,這個方法只能在objc_allocateClassPair函數(shù)與objc_registerClassPair之間調(diào)用仲闽。

BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) ;

//添加某個類的協(xié)議

BOOL class_addProtocol(Class cls, Protocol *protocol) ;

//添加某個類的屬性

BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount);

//替換某個類的屬性

void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount);

// runtime提供了幾個函數(shù)來確定一個對象的內(nèi)存區(qū)域是否可以被垃圾回收器掃描,以處理strong/weak引用.但通常情況下,我們不需要去主動調(diào)用這些方法逗概;在調(diào)用objc_registerClassPair時姻檀,會生成合理的布局袁波。

const uint8_t *class_getIvarLayout(Class cls);

const uint8_t *class_getWeakIvarLayout(Class cls);

void class_setIvarLayout(Class cls, const uint8_t *layout);

void class_setWeakIvarLayout(Class cls, const uint8_t *layout);

/*

method方法

*/

// 通過方法名返回方法

SEL method_getName(Method m) ;

// 獲取方法的實現(xiàn)地址

IMP method_getImplementation(Method m) ;

const char *method_getTypeEncoding(Method m) ;

// 獲取方法參數(shù)列表

unsigned int method_getNumberOfArguments(Method m);

char *method_copyReturnType(Method m) ;

char *method_copyArgumentType(Method m, unsigned int index) ;

void method_getReturnType(Method m, char *dst, size_t dst_len);

void method_getArgumentType(Method m, unsigned int index,

char *dst, size_t dst_len) ;

struct objc_method_description *method_getDescription(Method m) ;

// 修改方法實現(xiàn)

IMP method_setImplementation(Method m, IMP imp) ;

// 方法交換

void method_exchangeImplementations(Method m1, Method m2) ;

/*

ivar成員變量

*/

const char *ivar_getName(Ivar v) ;

const char *ivar_getTypeEncoding(Ivar v) ;

ptrdiff_t ivar_getOffset(Ivar v) ;

/*

property屬性

*/

const char *property_getName(objc_property_t property) ;

const char *property_getAttributes(objc_property_t property) ;

objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount);

char *property_copyAttributeValue(objc_property_t property, const char *attributeName);

/*

protocol協(xié)議

*/

BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other);

BOOL protocol_isEqual(Protocol *proto, Protocol *other);

const char *protocol_getName(Protocol *p);

struct objc_method_description protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod);

struct objc_method_description *protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount);

objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty);

objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount);

Protocol * __unsafe_unretained *protocol_copyProtocolList(Protocol *proto, unsigned int *outCount);

Protocol *objc_allocateProtocol(const char *name) ;

void objc_registerProtocol(Protocol *proto) ;

void protocol_addMethodDescription(Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod) ;

void protocol_addProtocol(Protocol *proto, Protocol *addition) ;

void protocol_addProperty(Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty);

const char **objc_copyImageNames(unsigned int *outCount) ;

const char *class_getImageName(Class cls) ;

const char **objc_copyClassNamesForImage(const char *image, unsigned int *outCount) ;

/*

sel  SEL

*/

const char *sel_getName(SEL sel);

SEL sel_getUid(const char *str);

SEL sel_registerName(const char *str);

BOOL sel_isEqual(SEL lhs, SEL rhs) ;

/*

objc對象的

*/

void objc_enumerationMutation(id) ;

void objc_setEnumerationMutationHandler(void (*handler)(id));

void objc_setForwardHandler(void *fwd, void *fwd_stret);

/*

imp

*/

IMP imp_implementationWithBlock(void *block);

void *imp_getBlock(IMP anImp);

BOOL imp_removeBlock(IMP anImp);

/*

運行時給分類添加/刪除屬性

*/

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);

id objc_getAssociatedObject(id object, const void *key);

void objc_removeAssociatedObjects(id object);

//API被客戶端的對象調(diào)用

id objc_loadWeak(id *location);

//返回值存儲(對象或者NULL)

id objc_storeWeak(id *location, id obj) ;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吭从,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌恶迈,老刑警劉巖涩金,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異暇仲,居然都是意外死亡步做,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門奈附,熙熙樓的掌柜王于貴愁眉苦臉地迎上來全度,“玉大人,你說我怎么就攤上這事桅狠∷显兀” “怎么了轿秧?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長咨堤。 經(jīng)常有香客問我菇篡,道長,這世上最難降的妖魔是什么一喘? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任驱还,我火速辦了婚禮,結(jié)果婚禮上凸克,老公的妹妹穿的比我還像新娘议蟆。我一直安慰自己,他們只是感情好萎战,可當我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布咐容。 她就那樣靜靜地躺著,像睡著了一般蚂维。 火紅的嫁衣襯著肌膚如雪虫啥。 梳的紋絲不亂的頭發(fā)上涂籽,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天树枫,我揣著相機與錄音团赏,去河邊找鬼舔清。 笑死体谒,一個胖子當著我的面吹牛抒痒,可吹牛的內(nèi)容都是我干的颁褂。 我是一名探鬼主播彩届,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼贮聂,長吁一口氣:“原來是場噩夢啊……” “哼吓懈!你這毒婦竟也來了耻警?” 一聲冷哼從身側(cè)響起榕栏,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤式曲,失蹤者是張志新(化名)和其女友劉穎吝羞,沒想到半個月后钧排,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體均澳,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡糟袁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年项戴,在試婚紗的時候發(fā)現(xiàn)自己被綠了周叮。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仿耽。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡水慨,死狀恐怖晰洒,靈堂內(nèi)的尸體忽然破棺而出谍珊,到底是詐尸還是另有隱情急侥,我是刑警寧澤贝润,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布打掘,位于F島的核電站尊蚁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望落君。 院中可真熱鬧穿香,春花似錦、人聲如沸绎速。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纹冤。三九已至洒宝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間萌京,已是汗流浹背雁歌。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留知残,地道東北人靠瞎。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像求妹,于是被迫代替她去往敵國和親乏盐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容