ios 获取属性的类型

如何在运行的时候动态获取到该属性的类型呢?

此方法获取属性的特性:property_copyAttributeValue


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(@"name: %@", [NSString stringWithFormat:@"%s", property_getName(o_t)]);
    NSLog(@"att: %@", [NSString stringWithFormat:@"%s", property_getAttributes(o_t)]);
    
}
2018-06-07 15:32:59.252063+0800 runtime[17026:450201] name: height_i
2018-06-07 15:32:59.252217+0800 runtime[17026:450201] att: Ti,V_height_i
2018-06-07 15:32:59.252316+0800 runtime[17026:450201] name: height_d
2018-06-07 15:32:59.252365+0800 runtime[17026:450201] att: Td,V_height_d
2018-06-07 15:32:59.252404+0800 runtime[17026:450201] name: height_f
2018-06-07 15:32:59.252434+0800 runtime[17026:450201] att: Tf,V_height_f
2018-06-07 15:32:59.252452+0800 runtime[17026:450201] name: weight
2018-06-07 15:32:59.252498+0800 runtime[17026:450201] att: T@"NSNumber",R,N,V_weight
2018-06-07 15:32:59.252518+0800 runtime[17026:450201] name: name
2018-06-07 15:32:59.252546+0800 runtime[17026:450201] att: T@"NSString",&,V_name
2018-06-07 15:32:59.252573+0800 runtime[17026:450201] name: family
2018-06-07 15:32:59.252612+0800 runtime[17026:450201] att: T@"NSString",&,V_family

如上可以打印特性值:

  1. 特性以 , 隔开
  2. 特性第一个是类型
  3. 特性最后一个是 V_name
  4. 特性中间为属性的修饰

特性的类型的规定:

image.png

特性的属性的修饰规定为:

image.png

官方文档为:* Declared Properties

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容