Runtime API應(yīng)用筆記

動(dòng)態(tài)創(chuàng)建一個(gè)類(參數(shù):父類,類名,額外的內(nèi)存空間)

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

注冊(cè)一個(gè)類(要在類注冊(cè)之前添加成員變量)

void objc_registerClassPair(Class cls)

銷毀一個(gè)類

void objc_disposeClassPair(Class cls)

獲取isa指向的Class

Class object_getClass(id obj)

設(shè)置isa指向的Class

Class object_setClass(id obj, Class cls)

判斷一個(gè)OC對(duì)象是否為Class

BOOL object_isClass(id obj)

判斷一個(gè)Class是否為元類


BOOL class_isMetaClass(Class cls)

獲取父類

Class class_getSuperclass(Class cls)

成員變量

獲取一個(gè)實(shí)例變量信息

Ivar class_getInstanceVariable(Class cls, const char *name)

拷貝實(shí)例變量列表(最后需要調(diào)用free釋放)

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

設(shè)置和獲取成員變量的值

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

動(dòng)態(tài)添加成員變量(已經(jīng)注冊(cè)的類是不能動(dòng)態(tài)添加成員變量的)

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

獲取成員變量的相關(guān)信息

const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)

屬性

獲取一個(gè)屬性

objc_property_t class_getProperty(Class cls, const char *name)

拷貝屬性列表(最后需要調(diào)用free釋放)

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

動(dòng)態(tài)添加屬性

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

動(dòng)態(tài)替換屬性

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

獲取屬性的一些信息

const char *property_getName(objc_property_t property)
const char *property_getAttributes(objc_property_t property)

方法

獲得一個(gè)實(shí)例方法、類方法


Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)

方法實(shí)現(xiàn)相關(guān)操作

IMP class_getMethodImplementation(Class cls, SEL name) 
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2)

拷貝方法列表(最后需要調(diào)用free釋放)

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

動(dòng)態(tài)添加方法

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

動(dòng)態(tài)替換方法

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

獲取方法的相關(guān)信息(帶有copy的需要調(diào)用free去釋放)

SEL method_getName(Method m)
IMP method_getImplementation(Method m)
const char *method_getTypeEncoding(Method m)
unsigned int method_getNumberOfArguments(Method m)
char *method_copyReturnType(Method m)
char *method_copyArgumentType(Method m, unsigned int index)

選擇器相關(guān)

const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)

用block作為方法實(shí)現(xiàn)

IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)

請(qǐng)關(guān)注我的專題 iOS - Developer - OC 進(jìn)階大全

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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