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) ;
iOS開發(fā)中Runtime.h / objc.h / message.h比較重要的聲明
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門奈附,熙熙樓的掌柜王于貴愁眉苦臉地迎上來全度,“玉大人,你說我怎么就攤上這事桅狠∷显兀” “怎么了轿秧?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長咨堤。 經(jīng)常有香客問我菇篡,道長,這世上最難降的妖魔是什么一喘? 我笑而不...
- 正文 為了忘掉前任驱还,我火速辦了婚禮,結(jié)果婚禮上凸克,老公的妹妹穿的比我還像新娘议蟆。我一直安慰自己,他們只是感情好萎战,可當我...
- 文/花漫 我一把揭開白布咐容。 她就那樣靜靜地躺著,像睡著了一般蚂维。 火紅的嫁衣襯著肌膚如雪虫啥。 梳的紋絲不亂的頭發(fā)上涂籽,一...
- 文/蒼蘭香墨 我猛地睜開眼贮聂,長吁一口氣:“原來是場噩夢啊……” “哼吓懈!你這毒婦竟也來了耻警?” 一聲冷哼從身側(cè)響起榕栏,我...
- 正文 年R本政府宣布打掘,位于F島的核電站尊蚁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望落君。 院中可真熱鬧穿香,春花似錦、人聲如沸绎速。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽纹冤。三九已至洒宝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間萌京,已是汗流浹背雁歌。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 《請停止無效努力》讀書筆記:第五章 選擇合適平臺:借助平臺成就不可取代的自己 第一篇 職業(yè)規(guī)劃應(yīng)規(guī)劃什么——90%...