美文网首页
RunTime系统学习《二》

RunTime系统学习《二》

作者: 个位数余额 | 来源:发表于2017-10-13 15:36 被阅读0次

    运行时中所有方法解析:

    1.设置获取对象的类

    object_getClass(id _Nullable obj)  //获取对象的类,参数传入对象

    object_setClass(id _Nullable obj, Class _Nonnull cls) //设置对象类型,参数:obj:被设置对象 cls:设置的值

    2.判断对象是不是类

    object_isClass(id _Nullable obj)  ,如果传入对象是类或者元类,返回yes,否则返回no

    3.获取或者设置对象的实例变量

    object_getIvar(id _Nullable obj, Ivar _Nonnull ivar) //获取实例变量,obj:想要获取的对象  ivar:想要获取的实例变量名称, 返回的是实例变量的值编码

    object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value) //设置实例变量名称,参数依次为 要设置的对象、实例变量名称、设置的值

    object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,

    id _Nullable value)  //设置值是强引用,即使是弱引用也会自动转换

    4.获取/修改对象的变量

    object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name,

    void * _Nullable value)  //修改对象的变量。参数: 对象、变量名、值

    object_setInstanceVariableWithStrongDefault(id _Nullable obj,

    const char * _Nonnull name,

    void * _Nullable value) //有内存管理,设置强引用

    object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,

    void * _Nullable * _Nullable outValue) //获取对象的变量值

    5.获取/设置类的父类

    class_getSuperclass(Class _Nullable cls) //获取父类

    class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper)

    6.获取类方法和对象方法

    class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)

    class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)

    class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)

    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);  //获取方法实现,返回指向方法的指针

    7.添加方法/替代方法

    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)

    8.方法有关的API

    method_getName(Method _Nonnull m)

    method_getImplementation(Method _Nonnull m)

    method_getTypeEncoding(Method _Nonnull m)

    method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len)

    method_setImplementation(Method _Nonnull m, IMP _Nonnull imp) //设置方法实现

    method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)  //交换方法实现

    9.实例变量有关API

    ivar_getName(Ivar _Nonnull v)

    ivar_getTypeEncoding(Ivar _Nonnull v)

    ivar_getOffset(Ivar _Nonnull v) //获取偏移量

    10 .属性有关API

    property_getName(objc_property_t _Nonnull property)

    property_getAttributes(objc_property_t _Nonnull property) //获取属性修饰特性

    相关文章

      网友评论

          本文标题:RunTime系统学习《二》

          本文链接:https://www.haomeiwen.com/subject/vnuzyxtx.html