類相關
- 動態創建一個類(參數:父類,類名,額爲的內存空間)
objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, size_t extraBytes)
- 註冊一個類(成員變量要在類註冊之前添加)
objc_registerClassPair(Class _Nonnull cls)
- 獲取isa指向的Class
object_getClass(id _Nullable obj)
- 修改isa的指向的Class
object_setClass(id _Nullable obj, Class _Nonnull cls)
- 判斷一個對象是否是Class對象
object_isClass(id _Nullable obj)
- 判斷Class是否是元類
class_isMetaClass(Class _Nullable cls)
- 獲取父類
class_getSuperclass(Class _Nullable cls)
成員變量相關
- 獲取實例變量
class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
- 拷貝實例變量列表
class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount)
- 設置和獲取成本變量的值
object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value)
object_getIvar(id _Nullable obj, Ivar _Nonnull ivar)
- 動態添加成員變量
class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size,
uint8_t alignment, const char * _Nullable types)
- 獲取成員變量名稱
ivar_getName(Ivar _Nonnull v)
- 獲取成員變量編碼
ivar_getTypeEncoding(Ivar _Nonnull v)
屬性相關
- 獲取一個屬性
class_getProperty(Class _Nullable cls, const char * _Nonnull name)
- 拷貝屬性列表
class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
- 動態添加屬性
class_addProperty(Class _Nullable cls, const char * _Nonnull name,
const objc_property_attribute_t * _Nullable attributes,
unsigned int attributeCount)
- 動態替換屬性
class_replaceProperty(Class _Nullable cls, const char * _Nonnull name,
const objc_property_attribute_t * _Nullable attributes,
unsigned int attributeCount)
- 獲取屬性信息
property_getName(objc_property_t _Nonnull property)
property_getAttributes(objc_property_t _Nonnull property)
舉個例子:
- (void)getProperty {
Person *person = [Person new];
person.age = 11;
objc_property_t property = class_getProperty(person.class, "age");
NSLog(@"\n name:%s--\n attri:%s",property_getName(property),property_getAttributes(property));
}
打印結果:
name:age--
attri:Ti,N,V_age
解釋下attri打印結果:
- T: 大寫 T 後面的字符代表該屬性的數據類型,i代表int
- V: 大寫 V 後面的字符代表該屬性對應的成員變量名稱,屬性age自動生成的成員變量_age
- N:代表nonatomic,如果屬性是atomic,則並不會打印出N
方法相關
- 獲取一個方法
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
- 方法實現相關操作
class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)
method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
- 拷貝方法列表(用完需調用free釋放)
class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
- 動態添加方法
class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
const char * _Nullable types)
- 動態替換方法
class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
const char * _Nullable types)
- 獲取方法相關信息(帶有copy的使用完需要調用free釋放)
method_getName(Method _Nonnull m)
method_getImplementation(Method _Nonnull m)
method_getTypeEncoding(Method _Nonnull m)
method_getNumberOfArguments(Method _Nonnull m)
method_copyReturnType
- 選擇器相關
sel_getName(SEL _Nonnull sel)
sel_registerName(const char * _Nonnull str)
- 用block作爲方法實現
imp_implementationWithBlock(id _Nonnull block)
imp_getBlock(IMP _Nonnull anImp)
imp_removeBlock(IMP _Nonnull anImp)