Runtime运行时之Class常用方法

原文地址

注: 此篇中记录的是以class开头的runtime函数

一、创建对象

创建一个对象

id class_createInstance(Class cls, size_t extraBytes)
复制代码
  • cls: 类型

  • extraBytes: 分配的内存大小

  • 返回值: cls类型的实例对象

例子:

size_t size = class_getInstanceSize([Person class]);
Person *person = class_createInstance([Person class], size);
复制代码

二、类名、父类、实例变量、成员变量、属性、实例方法、类方法、方法实现

获取指定类的类名 (char *类型字符串)

const char *class_getName(Class cls)
复制代码
  • cls: 类型

  • 返回值: 类名 - char * *类型的字符串

例子:

const char *name = class_getName([Person class])
复制代码

获取指定类的父类型 (Class类型)

Class class_getSuperclass(Class cls)
复制代码
  • cls: 类型

  • 返回值: 父类型 Class类型

例子:

Class class = class_getSuperclass([Person class]);
复制代码

获取一个指定类型的实例占用内存大小 (size_t 类型)

size_t class_getInstanceSize(Class cls)
复制代码
  • cls: 类型

  • 返回值: cls类型的一个实例对象占用内存大小

例子:

size_t size = class_getInstanceSize([Person class]);
复制代码

获取一个类中, 指定名称的实例成员变量的信息

Ivar class_getInstanceVariable(Class cls, const char *name)
复制代码
  • cls: 类型

  • name: 成员变量名(属性带 "_")

  • 返回值: 成员变量信息

例子:

Ivar ivar = class_getInstanceVariable([self class], "_person")
复制代码

获取属性的信息, 不用打_

objc_property_t class_getProperty(Class cls, const char *name)
复制代码
  • cls: 类

  • name: 属性名

  • 返回值: 属性信息

例子:

objc_property_t property = class_getProperty([self class], "person");
复制代码

获取对象方法信息 ( "-" 方法)

Method class_getInstanceMethod(Class cls, SEL name)
复制代码
  • cls: 类

  • name: 方法

  • 返回值: 对象方法信息 - * ### Method类型

例子:

Method method = class_getInstanceMethod([self class], @selector(methodName));
复制代码

获取类方法的信息 ( "+" 方法 )

Method class_getClassMethod(Class cls, SEL name)
复制代码
  • cls: 类

  • name: 方法

  • 返回值: 对象方法信息 - ### Method类型

例子:

Method method = class_getInstanceMethod([self class], @selector(methodName));
复制代码

获取方法的具体实现 (IMP)

IMP class_getMethodImplementation(Class cls, SEL name)
复制代码
  • cls: 类

  • name: 方法

  • 返回值: 方法的具体实现 ### IMP类型

例子:

IMP imp = class_getMethodImplementation([self class], @selector(methodName));
复制代码

获取方法的具体实现, 返回值类型为struct

IMP class_getMethodImplementation_stret(Class cls, SEL name)
复制代码
  • cls: 类

  • name: 方法

  • 返回值: 方法的具体实现 ### struct IMP类型

例子:

IMP imp = class_getMethodImplementation_stret([self class], @selector(methodName));
复制代码

三、成员变量列表、属性列表、方法列表、协议列表

获取成员变量列表

Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
复制代码
  • cls: 类

  • *outCount: 无符号整形变量的指针, 用于获取成员变量数量

  • 返回值: 成员变量列表

例子:

  unsigned int count;
  Ivar *ivarList = class_copyIvarList([self class], &count);
  for (int i = 0; i < count; i++) {
      Ivar ivar = ivarList[i];
      // 获取成员属性名
      NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
      NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
      NSLog(@"%@%@", type, name);
  }
复制代码

获取属性列表

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
复制代码
  • cls: 类型

  • *outCount: 无符号整形变量的指针, 用于获取属性数量

  • 返回值: 属性列表

例子:

  unsigned int count;
  objc_property_t *propertyList = class_copyPropertyList([self class], &count);
  for (int i = 0; i < count; i++) {
      objc_property_t property = propertyList[i];
      // 获取成员属性名
      NSString *name = [NSString stringWithUTF8String:property_getName(property)];
      NSString *type = [NSString stringWithUTF8String:property_getAttributes(property)];
      NSLog(@"%@%@", type, name);
  }
复制代码

获取方法列表

Method *class_copyMethodList(Class cls, unsigned int *outCount)
复制代码
  • cls: 类

  • *outCount: 无符号整形变量的指针, 用于获取方法数量

  • 返回值: 方法列表

例子:

  unsigned int count;
  Method *methodList = class_copyMethodList([self class], &count);
  for (int i = 0; i < count; i++) {
      Method method = methodList[i];
      NSLog(@"%s%s", __func__, sel_getName(method_getName(method)));
  }
复制代码

获取协议列表

Protocol *__unsafe_unretained *class_copyProtocolList(Class cls, unsigned int *outCount)
复制代码
  • cls: 类

  • *outCount: 无符号整形变量的指针, 用于获取协议数量

  • 返回值: 协议列表

例子:

  unsigned int count;
  Protocol *protocolList = class_copyProtocolList(class,&count);
  for (int i = 0; i < count; i++) {
      Protocol *protocol = protocolList[i];
      NSLog(@"%s%s", __func__, [self protocol_getName:protocol]);
  }
复制代码

四、add: 成员变量、属性、方法、协议

添加成员变量(添加成员变量只能在运行时创建的类,且不能为元类)

BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types)
复制代码
  • cls: 类

  • name: 成员变量名

  • size: 大小

  • alignment: 对齐方式

  • types: 参数类型

  • 返回值: YES: 添加成功 NO: 添加失败

例子:

  if (class_addIvar([Person class], "country", sizeof(NSString *), 0, "@")) {
    NSLog(@"%sadd ivar success", __func__);
  }else{
    NSLog(@"%sadd ivar fail", __func__);
  }
复制代码

添加属性

BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
复制代码
  • cls: 类

  • name: 属性名

  • attributes: 参数

  • attributeCount: 参数数量

  • 返回值: YES: 添加成功 NO: 添加失败

例子:

  objc_property_attribute_t type = { "T", "@\"NSString\"" };
  objc_property_attribute_t ownership = { "&", "N" }; // C = copy
  objc_property_attribute_t backingivar  = { "V", "" };
  objc_property_attribute_t attrs[] = { type, ownership, backingivar };

  if (class_addProperty([_person class], "country", attrs, 3)) {
      NSLog(@"%sadd Property success", __func__);
  }else{
      NSLog(@"%sadd Property fail", __func__);
  }
复制代码

添加方法

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
复制代码
  • cls: 类型

  • name: 方法

  • imp: 方法实现

  • types: 类型

  • 返回值: YES: 添加成功 NO: 添加失败

例子:

  if (class_addMethod([self class], NSSelectorFromString(@"runtimeTestMethod:"), class_getMethodImplementation([self class], NSSelectorFromString(@"runtimeTestMethod:")), "v@:@")) {
      NSLog(@"%sadd method success", __func__);
  }else{
      NSLog(@"%sadd method fail", __func__);
  }
复制代码

添加协议

BOOL class_addProtocol(Class cls, Protocol *protocol)
复制代码
  • cls: 类

  • protocol: 协议

  • 返回值: YES: 添加成功 NO: 添加失败

例子:

  if (class_addProtocol([_person class], @protocol(RuntimeBaseProtocol))) {
      NSLog(@"%sadd protocol success", __func__);
  }else{
      NSLog(@"%sadd protocol fail", __func__);
  }
复制代码

五、replace: 属性、方法

替换属性的信息(如果没有原属性会新建一个属性)

void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
复制代码
  • cls: 类

  • name: 属性名

  • attributes: 类型

  • attributeCount: 类型数量

  • 无返回值

例子:

  objc_property_attribute_t type = { "T", "@\"NSString\"" };
  objc_property_attribute_t ownership = { "C", "" }; // C = copy
  objc_property_attribute_t backingivar  = { "V", "" };
  objc_property_attribute_t attrs[] = { type, ownership, backingivar };

  class_replaceProperty([_person class], "country", attrs, 3);
复制代码

替代方法的实现

IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
复制代码
  • cls: 类

  • name: 被替代的方法

  • imp: 替代方法

  • types: 类型

  • 返回值: IMP

例子:

  class_replaceMethod([_person class], @selector(method1), class_getMethodImplementation([_person class], @selector(method2)), "v@:");
复制代码

六、Class 判断

判断类是否有实现指定方法

BOOL class_respondsToSelector(Class cls, SEL sel)
复制代码
  • cls: 类

  • sel: 方法

  • 返回值: YES: 已经实现 NO: 没有实现

例子:

  if (class_respondsToSelector([_person class], @selector(methodName))) {
      NSLog(@"%s %@ exist", __func__, NSStringFromClass(class));
  }else{
      NSLog(@"%s %@ non-exist", __func__, NSStringFromClass(class));
  }
复制代码

查看类是否为元类

BOOL class_isMetaClass(Class cls)
复制代码
  • cls: 类

  • 返回值: YES: 是元类 NO: 不是元类

例子:

  if (class_isMetaClass(class)) {
      NSLog(@"%s %@ isMetaClass", __func__, NSStringFromClass(class));
  }else{
      NSLog(@"%s %@ non-isMetaClass", __func__, NSStringFromClass(class));
  }
复制代码

查看类是否遵循指定协议

BOOL class_conformsToProtocol(Class cls, Protocol *protocol)
复制代码
  • cls: 类

  • protocol: 协议

  • 返回值: YES: 已经遵循指定协议 NO: 没有遵循指定协议

例子:

  if (class_conformsToProtocol([Person class], NSProtocolFromString(@"RuntimeBaseProtocol"))) {
      NSLog(@"%s %@ conformsToProtocol %@", __func__, NSStringFromClass(class), NSStringFromProtocol(protocol));
      return YES;
  }else{
      NSLog(@"%s %@ non-conformsToProtocol %@", __func__, NSStringFromClass(class), NSStringFromProtocol(protocol));
      return NO;
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值