-
以下列舉了使用 Runtime 時(shí)常用到的 部分API ,并非全部
| Runtime 類 相關(guān) API |
|---|
| 動(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) |
| Runtime 成員變量 相關(guān) API |
|---|
| 獲取一個(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) |
| Runtime 屬性 相關(guān) API |
|---|
| 獲取一個(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) |
| Runtime 方法相關(guān) API |
|---|
| 獲得一個(gè)實(shí)例方法 |
| Method class_getInstanceMethod(Class cls, SEL name) |
| 獲得一個(gè)類方法方法 |
| Method class_getClassMethod(Class cls, SEL name) |
| 獲取方法實(shí)現(xiàn) |
| IMP class_getMethodImplementation(Class cls, SEL name) |
| 設(shè)置方法實(shí)現(xiàn) |
| IMP method_setImplementation(Method m, IMP imp) |
| 交換方法實(shí)現(xiàn) |
| 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) |
| 獲取方法名 |
| SEL method_getName(Method m) |
| 獲取方法實(shí)現(xiàn) |
| IMP method_getImplementation(Method m) |
| 獲取方法編碼 |
| const char *method_getTypeEncoding(Method m) |
| 獲取方法參數(shù)個(gè)數(shù) |
| unsigned int method_getNumberOfArguments(Method m) |
| 獲取方法返回類型(需要調(diào)用free去釋放) |
| char *method_copyReturnType(Method m) |
| 獲取指定位置參數(shù)類型(需要調(diào)用free去釋放) |
| char *method_copyArgumentType(Method m, unsigned int index) |