runtime属性与属性类型字符串

在runtime中,属性property具有一系列特性attribute,如下是attribute的数据结构

typedef struct {
    const char *name;           /**< 特姓名*/
    const char *value;          /**< 特性值(通常为空) */
} objc_property_attribute_t;

在runtime中取得属性特性的函数为

objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount)

这个函数返回的是属性的特性数组

同样还能使用函数

const char *property_getAttributes(objc_property_t property) 

获得关于属性特性描述的C字符串,在这个字符串中:

  • 每个特性都以逗号“,”分隔,特性名和特性值前后连接
  • 字符串以属性类型开头,以对应成员变量名称结尾
  • 特性名一般使用属性类型编码表示,大多数特性的只有特性名,其特性值通常为空

例如,如下类

@interface Model : NSObject 

@property NSString *str2;
@property (nonatomic, copy)NSDictionary *dic1;

@end

具有两个属性str2与dic1,其属性的特性描述分别为

2016-08-06 16:05:46.625 RuntimeExercise[1102:87334] 属性str2的特性描述为T@"NSString",&,V_str2
2016-08-06 16:05:46.625 RuntimeExercise[1102:87334] 属性dic1的特性描述为T@"NSDictionary",C,N,V_dic1

按照特性描述可以看到属性str2有三个属性

特性1: name:T value:@"NSString"           
特性2: name:& value:空                     
特性3: name:V value:_str2                 

而属性dic1则有四个属性,因为dic1指定了属性选项和非原子性,而str2仅默认属性选项strong

特性1: name:T value:@"NSDictionary"
特性2: name:C value:空
特性3: name:N value:空
特性4: name:V value:_dic1

其中T、C、N、V是属性类型编码,因此可以总结出常用的特性

属性类型:      name:T           value:类型名
属性选项:      name:&、C、W      value:空
原子性:        name:N          value:空
对应成员变量:   name:V            value:成员变量名

示例代码如下:

@interface Model : NSObject

@property NSString *str2;
@property (nonatomic, copy)NSDictionary *dic1;

@end

@implementation Model

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        unsigned int outCount = 0;
        objc_property_t *properties = class_copyPropertyList([Model class], &outCount);
        for(int i = 0; i < outCount; i++)
        {
            objc_property_t property = properties[i];
            const char *name = property_getName(property);
            const char *attri = property_getAttributes(property);
            NSLog(@"属性%s的特性描述为%s", name, attri);
        
            unsigned int attrCount = 0;
            objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
            for(int i = 0; i < attrCount; i++)
            {
                objc_property_attribute_t attr = attrs[i];
                const char *name = attr.name;
                const char *value = attr.value;
                NSLog(@"name:%s  value:%s",name,value);
            }
            free(attrs)
            NSLog(@"\n");
        }
        free(properties);
    }
    return 0;
}

运行结果:

2016-08-06 16:40:30.171 RuntimeExercise[1185:99954] 属性str2的特性描述为T@"NSString",&,V_str2
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] name:T  value:@"NSString"
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] name:&  value:
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] name:V  value:_str2
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] 
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] 属性dic1的特性描述为T@"NSDictionary",C,N,V_dic1
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:T  value:@"NSDictionary"
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:C  value:
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:N  value:
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:V  value:_dic1
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] 
Program ended with exit code: 0

有关属性以及属性编码知识,可以阅读相关官方文档-Declared Properties

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,696评论 33 466
  • 原文出处:南峰子的技术博客 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了...
    _烩面_阅读 1,320评论 1 5
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,281评论 0 4
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 1,471评论 0 8
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,274评论 30 472