前一段时间学习了Runtime,对类和对象的结构,和一些消息转发有一些自己的理解,现在希望简单的应用下,就决定自己写一个简单的JSON与Model的相互转化,现在总结下。
建议查看
- 参考资料 :Runtime学习笔记
- 项目地址: LYModelData
观察下面这个JSON数据和Model数据
1 2 3 4 5 6 7 |
NSString *girlFriend = @"白菜"; id parmenters = @{ @"girlFriend":girlFriend, @"age":@22.1, @"name":@"Lastdays", @"time":@"2016-03-18 5:55:49 +0000" }; |
1 2 3 4 5 6 7 8 |
@interface Model : NSObject @property NSNumber *age; @property NSString *name; @property NSString *girlFriend; @property NSData *time; @end |
开始的时候仔细想了一下,如何能够动态的去添加属性值,并且根据对应的属性进行赋值,还要保证类型正确,这是我最开始考虑的问题。但是最核心问题就是动态实现。
我们一步一步来解决问题,首先我们先获取Model属性,取得Model的一些信息
获取Model属性
runtime提供了class_copyPropertyList来获取属性列表,OK,我们可以来看一下用它获取的数据是什么样的?查看runtime源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/*********************************************************************** * class_copyPropertyList. Returns a heap block containing the * properties declared in the class, or nil if the class * declares no properties. Caller must free the block. * Does not copy any superclass's properties. **********************************************************************/ objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount) { old_property_list *plist; uintptr_t iterator = 0; old_property **result = nil; unsigned int count = 0; unsigned int p, i; if (!cls) { if (outCount) *outCount = 0; return nil; } mutex_locker_t lock(classLock); iterator = 0; while ((plist = nextPropertyList(cls, &iterator))) { count += plist->count; } if (count > 0) { result = (old_property **)malloc((count+1) * sizeof(old_property *)); p = 0; iterator = 0; while ((plist = nextPropertyList(cls, &iterator))) { for (i = 0; i < plist->count; i++) { result[p++] = property_list_nth(plist, i); } } result[p] = nil; } if (outCount) *outCount = count; return (objc_property_t *)result; } |
建议查看
- 参考资料 :Runtime学习笔记
- 项目地址: LYModelData
观察下面这个JSON数据和Model数据
1 2 3 4 5 6 7 |
NSString *girlFriend = @"白菜"; id parmenters = @{ @"girlFriend":girlFriend, @"age":@22.1, @"name":@"Lastdays", @"time":@"2016-03-18 5:55:49 +0000" }; |
1 2 3 4 5 6 7 8 |
@interface Model : NSObject @property NSNumber *age; @property NSString *name; @property NSString *girlFriend; @property NSData *time; @end |
开始的时候仔细想了一下,如何能够动态的去添加属性值,并且根据对应的属性进行赋值,还要保证类型正确,这是我最开始考虑的问题。但是最核心问题就是动态实现。
我们一步一步来解决问题,首先我们先获取Model属性,取得Model的一些信息
获取Model属性
runtime提供了class_copyPropertyList来获取属性列表,OK,我们可以来看一下用它获取的数据是什么样的?查看runtime源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/*********************************************************************** * class_copyPropertyList. Returns a heap block containing the * properties declared in the class, or nil if the class * declares no properties. Caller must free the block. * Does not copy any superclass's properties. **********************************************************************/ objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount) { old_property_list *plist; uintptr_t iterator = 0; old_property **result = nil; unsigned int count = 0; unsigned int p, i; if (!cls) { if (outCount) *outCount = 0; return nil; } mutex_locker_t lock(classLock); iterator = 0; while ((plist = nextPropertyList(cls, &iterator))) { count += plist->count; } if (count > 0) { result = (old_property **)malloc((count+1) * sizeof(old_property *)); p = 0; iterator = 0; while ((plist = nextPropertyList(cls, &iterator))) { for (i = 0; i < plist->count; i++) { result[p++] = property_list_nth(plist, i); } } result[p] = nil; } if (outCount) *outCount = count; return (objc_property_t *)result; } |
1 |