前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ios OC 获取所有属性、变量 class_getProperty class_copyPropertyList class_copyIvarList

ios OC 获取所有属性、变量 class_getProperty class_copyPropertyList class_copyIvarList

作者头像
onety码生
发布2018-11-21 11:51:29
5.2K5
发布2018-11-21 11:51:29
举报
文章被收录于专栏:码生

class_copyPropertyList

  • 看到名字可以看出来他是来获取属性的
  • 只能获取到 @property 声明的属性

class_copyIvarList

  • 用来获取所有的变量的
  • 获取所有的变量,当然包括因 @property 修饰而自动产生的变量 _name

总结

  • class_copyPropertyList 只能获取到 @property 声明的属性
  • class_copyIvarList 用来获取所有的变量的
  • 但是以上两个方法都只能获取到当前类的属性和变量(也就是说获取不到父类的属性和变量)

验证

以上的结论通过一段代码可以验证

首先声明类:

代码语言:javascript
复制
@interface KK : NSObject
{
    int _i;
}

@property NSString * name;

@property NSString * family;

@end

@implementation KK

- (NSString *)description {
    return [NSString stringWithFormat:@"KK -> description:  name: %@", self.name];
}

@end

@interface MM : KK

@property int age;

@end

@implementation MM

@end
第一段代码:
代码语言:javascript
复制
KK * k = [[KK alloc] init];
        
unsigned int a;

objc_property_t * result = class_copyPropertyList(object_getClass(k), &a);

for (unsigned int i = 0; i < a; i++) {
    objc_property_t o_t =  result[i];
    NSLog(@"%@", [NSString stringWithFormat:@"%s", property_getName(o_t)]);
}

free(result);

Ivar * iv = class_copyIvarList(object_getClass(k), &a);

for (unsigned int i = 0; i < a; i++) {
    Ivar i_v = iv[i];
    NSLog(@"%@", [NSString stringWithFormat:@"%s", ivar_getName(i_v)]);
}

free(iv);
代码语言:javascript
复制
2018-06-07 14:19:25.184787+0800 runtime[16059:422758] name
2018-06-07 14:19:25.184884+0800 runtime[16059:422758] family
2018-06-07 14:19:25.184940+0800 runtime[16059:422758] _i
2018-06-07 14:19:25.184960+0800 runtime[16059:422758] _name
2018-06-07 14:19:25.184977+0800 runtime[16059:422758] _family

如上可以验证

  • class_copyPropertyList 只能获取 @property 修饰的属性
第二段代码
代码语言:javascript
复制
KK * k = [[MM alloc] init];
        
unsigned int a;

objc_property_t * result = class_copyPropertyList(object_getClass(k), &a);

for (unsigned int i = 0; i < a; i++) {
    objc_property_t o_t =  result[i];
    NSLog(@"%@", [NSString stringWithFormat:@"%s", property_getName(o_t)]);
}

free(result);

Ivar * iv = class_copyIvarList(object_getClass(k), &a);

for (unsigned int i = 0; i < a; i++) {
    Ivar i_v = iv[i];
    NSLog(@"%@", [NSString stringWithFormat:@"%s", ivar_getName(i_v)]);
}

free(iv);
代码语言:javascript
复制
2018-06-07 14:21:26.258232+0800 runtime[16089:423658] age
2018-06-07 14:21:26.258295+0800 runtime[16089:423658] _age
  • 只获取当前类的属性和方法

获取指定类的属性以及父类的所有属性

代码语言:javascript
复制
/**
 获取指定类的属性
 
 @param cls 被获取属性的类
 @return 属性名称 [NSString *]
 */
NSArray * getClassProperty(Class cls) {
    
    if (!cls) return @[];
    
    NSMutableArray * all_p = [NSMutableArray array];
    
    unsigned int a;
    
    objc_property_t * result = class_copyPropertyList(cls, &a);
    
    for (unsigned int i = 0; i < a; i++) {
        objc_property_t o_t =  result[i];
        [all_p addObject:[NSString stringWithFormat:@"%s", property_getName(o_t)]];
    }
    
    free(result);
    
    return [all_p copy];
}


/**
 获取指定类(以及其父类)的所有属性
 
 @param cls 被获取属性的类
 @param until_class 当查找到此类时会停止查找,当设置为 nil 时,默认采用 [NSObject class]
 @return 属性名称 [NSString *]
 */
NSArray * getAllProperty(Class cls, Class until_class) {
    
    Class stop_class = until_class ?: [NSObject class];
    
    if (class_getSuperclass(cls) == stop_class) return @[];
    
    NSMutableArray * all_p = [NSMutableArray array];
    
    [all_p addObjectsFromArray:getClassProperty(cls)];
    
    if (class_getSuperclass(cls) == stop_class) {
        return [all_p copy];
    } else {
        [all_p addObjectsFromArray:getAllProperty([cls superclass], stop_class)];
    }
    
    return [all_p copy];
}

获取指定类以及其父类所有的变量

代码语言:javascript
复制
/**
 获取指定类的变量
 
 @param cls 被获取变量的类
 @return 变量名称集合 [NSString *]
 */
NSArray * getClassIvar(Class cls) {
    
    if (!cls) return @[];
    
    NSMutableArray * all_p = [NSMutableArray array];
    
    unsigned int a;
    
    Ivar * iv = class_copyIvarList(cls, &a);
    
    for (unsigned int i = 0; i < a; i++) {
        Ivar i_v = iv[i];
        [all_p addObject:[NSString stringWithFormat:@"%s", ivar_getName(i_v)]];
    }
    
    free(iv);
    
    return [all_p copy];
}


/**
 获取指定类(以及其父类)的所有变量
 
 @param cls 被获取变量的类
 @param until_class 当查找到此类时会停止查找,当设置为 nil 时,默认采用 [NSObject class]
 @return 变量名称集合 [NSString *]
 */
NSArray * getAllIvar(Class cls, Class until_class) {
    
    Class stop_class = until_class ?: [NSObject class];
    
    if (class_getSuperclass(cls) == stop_class) return @[];
    
    NSMutableArray * all_p = [NSMutableArray array];
    
    [all_p addObjectsFromArray:getClassIvar(cls)];
    
    if (class_getSuperclass(cls) == stop_class) {
        return [all_p copy];
    } else {
        [all_p addObjectsFromArray:getAllIvar([cls superclass], stop_class)];
    }
    
    return [all_p copy];
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.06.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • class_copyPropertyList
  • class_copyIvarList
  • 总结
  • 验证
    • 第一段代码:
      • 第二段代码
      • 获取指定类的属性以及父类的所有属性
      • 获取指定类以及其父类所有的变量
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档