Runtime常用API

作者: YY_Lee | 来源:发表于2019-03-04 17:56 被阅读1次

Runtime API demo

类相关
  • 动态创建一个类(参数:父类,类名,额为的内存空间)
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)

相关文章

  • runtime02-常用API

    runtime常用API runtime API01-类相关 runtime API01-类相关-事例01 run...

  • 基础篇

    Runtime之必备C知识 Runtime之类的本质 Runtime之消息处理策略 Runtime之常用API 进...

  • Runtime 相关 API

    以下列举了使用 Runtime 时常用到的 部分API ,并非全部 Runtime 类 相关 API动态创建一个类...

  • 11.runtime 理解 和 常用点

    问题 1.runtime 个人理解2.runtime 常用api3.runtime 日常运用解决问题4.runti...

  • 常用Runtime API

    前言:本文只是分类列举一些常用Runtime API?一些Runtime 常用场景 1. 类 动态创建一个类 注册...

  • 常用 Runtime API

    记录一下常用的Runtime API 类: 成员变量: 属性: 方法:

  • Runtime:常用API

    目录一,类二,成员变量三,属性四,方法五,交换函数 一,类 1,创建和销毁 2,获取和设置 二,成员变量 1,获取...

  • Runtime常用API

    Runtime API demo 类相关 动态创建一个类(参数:父类,类名,额为的内存空间) 注册一个类(成员变量...

  • Runtime常用API

    类相关 获取isa指向的Class 方法实现:Class object_getClass(id obj) { i...

  • iOS底层原理 - Runtime-03

    Runtime API - 类 Runtime API – 成员变量 Runtime API – 属性 Runt...

网友评论

    本文标题:Runtime常用API

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