iOS点点滴滴

Runtime常用API

2019-03-04  本文已影响1人  YY_Lee

Runtime API demo

类相关
objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, size_t extraBytes) 
objc_registerClassPair(Class _Nonnull cls) 
object_getClass(id _Nullable obj) 
object_setClass(id _Nullable obj, Class _Nonnull cls) 
object_isClass(id _Nullable obj)
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打印结果:

方法相关
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) 
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)
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)
imp_implementationWithBlock(id _Nonnull block)
imp_getBlock(IMP _Nonnull anImp)
imp_removeBlock(IMP _Nonnull anImp)
上一篇下一篇

猜你喜欢

热点阅读