iOS获取类的属性列表

通过实例讲解

@interface DemoObject : NSObject

@property (strong, nonatomic,readonly) NSString *name;
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (copy, nonatomic) NSDictionary *product;
@property (assign, atomic) NSUInteger count;
@property (weak, nonatomic) DemoObject *object;

@end
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList([DemoObject class], &count);
    for(int i = 0; i < count; i++)
    {
        objc_property_t property = properties[i];

        NSLog(@"name:%s",property_getName(property));
        NSLog(@"attributes:%s",property_getAttributes(property));

    }
    free(properties);

打印信息如下:

2015-04-28 14:59:16.421 AppTest[11137:94568] name:name

2015-04-28 14:59:16.422 AppTest[11137:94568] attributes:[email protected]"NSString",R,N,V_name

2015-04-28 14:59:16.422 AppTest[11137:94568] name:dataSource

2015-04-28 14:59:16.422 AppTest[11137:94568] attributes:[email protected]"NSMutableArray",&,N,V_dataSource

2015-04-28 14:59:16.422 AppTest[11137:94568] name:product

2015-04-28 14:59:16.422 AppTest[11137:94568] attributes:[email protected]"NSDictionary",C,N,V_product

2015-04-28 14:59:16.422 AppTest[11137:94568] name:count

2015-04-28 14:59:16.422 AppTest[11137:94568] attributes:TQ,V_count

2015-04-28 14:59:16.422 AppTest[11137:94568] name:object

2015-04-28 14:59:16.422 AppTest[11137:94568] attributes:[email protected]"DemoObject",W,N,V_object

说明:

如果我们要获取字符串,要通过UTF8编码获取,如下所示:

NSString *name = [NSString stringWithUTF8String:property_getName(property)];
NSString *attributes = [NSString stringWithUTF8String:property_getAttributes(property)];

属性的特性是固定格式,以逗号隔开,通常是[email protected]"类型"开头,V_属性名称结尾的格式,中间的见下表描述:

You can use the property_getAttributes function to discover the name, the @encode type string of a property, and other attributes of the property.

The string starts with a T followed by the @encode type and a comma, and finishes with a V followed by the name of the backing instance variable. Between these, the attributes are specified by the following descriptors, separated by commas:

还有一些其他类型的属性实例,可以搜索苹果文档“Property Attribute Description Examples”

用途:

1.我们在发送网络请求的时候,收到服务器回复的数据模型以后,不需要在每个数据模型里面去解析json字典为我们的模型类,我们可以通过一个基类来实现这个功能,所有的模型都继承这个基类就OK了,这个功能就可以简单地通过属性列表来获取属性名称,属性类型(属性类型对应json模型的键)然后赋值就可以了。

2.自定义类的序列化和反序列化。

时间: 2024-08-10 08:17:55

iOS获取类的属性列表的相关文章

iOS 反射获取类的属性列表

// 获取对象所有属性: - (NSArray*)propertyKeys { unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:outCount]; for (i = 0; i < outCount; i+

iOS数据存储之属性列表理解

iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起驻留在内存中,一旦程序运行结束从内存中退出后,这些数据也就相应消失了.等到再次运行程序的时候,之前的那些数据又要重新计算.但是对于一些应用,我们需要将程序产生的数据持久的保存起来,使得应用重启之后这些数据不会丢失,这时候就需要用到数据的持久化技术. 在iOS设备上实现数据持久化存储的方式有很多中机制

ios 获取屏幕的属性和宽度

1.app尺寸,去掉状态栏 CGRect r = [ UIScreen mainScreen ].applicationFrame; r=0,20,320,460 另外:self.view.bounds.size 2.屏幕尺寸 CGRect rx = [ UIScreen mainScreen ].bounds; r=0,0,320,480 3.状态栏尺寸 CGRect rect; rect = [[UIApplication sharedApplication] statusBarFrame]

iOS数据持久化之---属性列表

属性列表(plist) iOS提供了一种plist格式的文件(属性列表)用于存储轻量级的数据,并且只能保存NSDictionary.NSArray.NSString.NSNumber.Boolean.NSData.NSDate 类型的数据.将这些类型的数据保存为plist格式文件,该格式保存的数据可以直接使用NSDictionary和NSArray读取 (一).使用NSUserDefault 实现持久化   下面来看下 NSUserDefault 本地保存的位置,数据持久化之沙盒目录有提及.Li

iOS开发备忘录:属性列表文件数据持久化

原文:http://www.cnblogs.com/wzk89/p/3939782.html 属性列表文件是一种XML文件,Foundation框架中的数组和字典等都可以于属性列表文件相互转换. NSArray类常用读写属性列表文件的方法: +arrayWithContentsOfFile:类级构造方法,用于从属性列表文件中读取数据,创建NSArray对象. -initWithContentsOfFile:实例构造方法,用于从属性列表文件中读取数据,创建NSArray对象. -writeToFi

反射之获取类,属性,方法,接口,父类等

获取类有三种方法 实体类和接口 public interface Person { public void sayHi(); } public class Student  implements Person{ private String id; private String name; private int age; public int sex=1; /*get和set方法省略*/ } Class c1 = Student.class; Class c2=Class.forName("c

通过OC运行时(runtime)获得类的属性列表

最近一段时间在研究OC的运行时机制,碰到一个叫property_getAttributes函数,再此对其用法进行简单的总结. property_getAttributes主要用于获取一个类的property即该property的属性数据,也叫metadata(元数据),涉及到得运行时函数包括class_copyPropertyList,property_getName和propert_getAttributes 大体用法如下: #import <objc/runtime.h> ...... -

利用Lambda获取类中属性名称

1 public class TypeInfoHelper 2 { 3 public static string GetPropertyName<T>(Expression<Func<T, dynamic>> property) 4 { 5 var propertyName = string.Empty; 6 var body = property.Body; 7 if (body.NodeType == ExpressionType.Convert) 8 { 9 va

通过反射,获取类的属性

http://stackoverflow.com/questions/3723934/using-propertyinfo-to-find-out-the-property-type https://www.codewars.com/kata/56c22c5ae8b139416c00175d/train/csharp using StriveObjects; using System.Reflection; using System; public class Strive { public s