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

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

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

NSClassFromString;

NSSelectorFromString;

performSelector:

// 動(dòng)態(tài)方法處理--提供再一次實(shí)現(xiàn)方法的機(jī)會(huì). 在沒有真正提供方法實(shí)現(xiàn)的,并提供了消息轉(zhuǎn)發(fā)機(jī)制的情況下,return YES 表示不進(jìn)行后續(xù)的消息轉(zhuǎn)發(fā),NO表示要進(jìn)行后續(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: // 不能識(shí)別的消息分發(fā)中心----在方法的實(shí)現(xiàn)中將消息轉(zhuǎn)發(fā)個(gè)其他對(duì)象去實(shí)現(xiàn).

invokeWithTarget: // 轉(zhuǎn)發(fā)的消息通過這個(gè)函數(shù)來實(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  ;    //實(shí)例變量的大小

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

  struct objc_method_list **methodLists  ;    //方法列表,存儲(chǔ)的是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ù)指針,指向方法具體實(shí)現(xiàn)的指針..也即是selector的address

} ;

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

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

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

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

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

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

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

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

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

// 三個(gè)參數(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對(duì)象

*/

// 返回指定對(duì)象的一份拷貝

id object_copy(id obj, size_t size);

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

id object_dispose(id obj);

// 獲取實(shí)例對(duì)象的所屬的類

Class object_getClass(id obj) ;

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

Class object_setClass(id obj, Class cls) ;

// 獲取實(shí)例對(duì)象的所屬類的類名

const char *object_getClassName(id obj);

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

void *object_getIndexedIvars(id obj);

// 獲取實(shí)例對(duì)象的成員變量

id object_getIvar(id obj, Ivar ivar) ;

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

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

// 修改類實(shí)例的實(shí)例變量的值

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

// 獲取對(duì)象實(shí)例變量的值

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

// 返回指定類的元類

id objc_getMetaClass(const char *name);

// 返回指定類的類定義

id objc_lookUpClass(const char *name);

// 返回實(shí)例對(duì)象的類

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);

// 獲取已注冊(cè)的類定義的列表

int objc_getClassList(Class *buffer, int bufferCount);

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

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);

// 獲取該類實(shí)例對(duì)象大小

size_t class_getInstanceSize(Class cls) ;

// 獲取類中指定名稱實(shí)例對(duì)象的信息

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

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

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

// 獲取整個(gè)成員變量列表

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

// 獲取實(shí)例方法.

Method class_getInstanceMethod(Class cls, SEL name);

// 獲取類方法.

Method class_getClassMethod(Class cls, SEL name);

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

IMP class_getMethodImplementation(Class cls, SEL name) ;

// 返回方法的具體實(shí)現(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);

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

BOOL class_conformsToProtocol(Class cls, Protocol *protocol) ;

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

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

/*

object對(duì)象

*/

// 獲取指定的屬性

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

// 獲取屬性列表

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

// 創(chuàng)建實(shí)例對(duì)象

id class_createInstance(Class cls, size_t extraBytes);

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

id objc_constructInstance(Class cls, void *bytes) ;

// 銷毀類實(shí)例

void *objc_destructInstance(id obj) ;

/*

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

*/

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

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

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

void objc_registerClassPair(Class cls) ;

// 復(fù)制一份類

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

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

void objc_disposeClassPair(Class cls) ;

/*

class類

*/

//添加某個(gè)類的方法

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

//替換某個(gè)類的方法

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

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

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

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

BOOL class_addProtocol(Class cls, Protocol *protocol) ;

//添加某個(gè)類的屬性

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

//替換某個(gè)類的屬性

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

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

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) ;

// 獲取方法的實(shí)現(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) ;

// 修改方法實(shí)現(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對(duì)象的

*/

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);

/*

運(yùn)行時(shí)給分類添加/刪除屬性

*/

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被客戶端的對(duì)象調(diào)用

id objc_loadWeak(id *location);

//返回值存儲(chǔ)(對(duì)象或者NULL)

id objc_storeWeak(id *location, id obj) ;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容