自动解析复杂类的属性 实现归档或者进行序列化 反序列话的时候为每一个属性添加序列化方法的繁琐...
生活随笔
收集整理的這篇文章主要介紹了
自动解析复杂类的属性 实现归档或者进行序列化 反序列话的时候为每一个属性添加序列化方法的繁琐...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近學習了歸檔與序列化的操作,在進行操作的時候需要為每一個類寫上歸檔以及反歸檔相關的操作? 但屬性較多時這是一項費力不討好的事情? 遂自己寫了個工具類,能夠實現自動根據屬性個數進行自動序列反序列化操作
主要運用了ios里runtime的方法與屬性? 其實在這里是和java的反射機制是一樣的。
代碼總共分為了兩種?
????1.利用分類的方式實現 只需要拷貝分類進入工程? 不需要進行其他任何操作? 因為分類會對所有的model起作用? 所以還有第二種方式供參考
????2.需要將NSObject繼承改為ZJAutoCoding繼承
?代碼如下,恭敬敬上~~~
1 // 2 // NSObject+ZJAutoCoding.h 3 // 4 // Created by Jason_Msbaby on 15/10/13. 5 // Copyright ? 2015年 張杰. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 10 @interface NSObject (ZJAutoCoding) 11 12 @end 13 14 15 16 17 18 19 // 20 // NSObject+ZJAutoCoding.m 21 // 22 // Created by Jason_Msbaby on 15/10/13. 23 // Copyright ? 2015年 張杰. All rights reserved. 24 // 25 26 #import "NSObject+ZJAutoCoding.h" 27 #import <objc/runtime.h> 28 @implementation NSObject (ZJAutoCoding) 29 30 31 - (instancetype)initWithCoder:(NSCoder *)coder 32 { 33 //獲取所有屬性 34 unsigned int count = 0; 35 objc_property_t *pros = class_copyPropertyList(self.class, &count); 36 //遍歷屬性 37 for (int i = 0; i < count; i++) { 38 objc_property_t pro = pros[i]; 39 //當前屬性對應的名 40 NSString *proName = [NSString stringWithUTF8String:property_getName(pro)]; 41 unsigned int c = 0; 42 //獲取所有特征 43 objc_property_attribute_t *attrs = property_copyAttributeList(pro, &c); 44 //遍歷所有特征 45 for (int j = 0; j < c; j++ ) { 46 objc_property_attribute_t attr = attrs[j]; 47 //這里的V指的是知道屬性對應的成員變量的名稱 例如name屬性 在這里的特征值則為 V:_name 我們需要的是_name 48 if (strcmp(attr.name, "V") == 0) { 49 //然后我們需要獲取實例進行賦值操作 50 Ivar ivar = class_getInstanceVariable(self.class, attr.value); 51 object_setIvar(self, ivar,[coder decodeObjectForKey:proName]); 52 } 53 } 54 free(attrs); 55 } 56 free(pros); 57 return [self init]; 58 } 59 60 -(void)encodeWithCoder:(NSCoder *)aCoder{ 61 //獲取所有的屬性 62 unsigned int count = 0; 63 objc_property_t *pros = class_copyPropertyList([self class], &count); 64 //遍歷屬性 65 for (int i = 0; i < count; i++) { 66 objc_property_t pro = pros[i]; 67 unsigned int c = 0; 68 //獲取實例變量的名稱 69 NSString *proName = [NSString stringWithUTF8String:property_getName(pro)]; 70 //通過當前的屬性 獲取該屬性的特征 71 objc_property_attribute_t *attrs = property_copyAttributeList(pro, &c); 72 //遍歷特征 能夠得到其類型以及數值等內容 73 for (int j = 0; j < c; j++) { 74 objc_property_attribute_t attr = attrs[j]; 75 if (strcmp(attr.name, "V") == 0) {//如果是特征的名稱 76 //獲取當前類對應特征名稱的實例變量 77 Ivar ivar = class_getInstanceVariable(self.class, attr.value); 78 //得到該實例變量的數值 79 id object = object_getIvar(self, ivar); 80 NSLog(@"%@",object); 81 //歸檔 82 [aCoder encodeObject:object forKey:proName]; 83 } 84 85 } 86 free(attrs); 87 } 88 free(pros); 89 } 90 91 @end 92 93 94 -------------繼承的方式如下------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 95 96 97 // 98 // ZJAutoCoding.h 99 // 100 // Created by Jason_Msbaby on 15/10/13. 101 // Copyright ? 2015年 張杰. All rights reserved. 102 // 103 104 #import <Foundation/Foundation.h> 105 106 /** 107 自動歸檔序列化 108 需要繼承該類 109 110 - returns: 111 */ 112 @interface ZJAutoCoding : NSObject <NSCoding> 113 114 /** 115 * 全自動動態加載屬性 實現反歸檔 116 * 117 * @param coder 118 * 119 * @return 120 */ 121 - (instancetype)initWithCoder:(NSCoder *)coder; 122 /** 123 * 全自動動態加載屬性 實現歸檔 124 * 125 * @param aCoder 126 */ 127 -(void)encodeWithCoder:(NSCoder *)aCoder; 128 @end 129 130 131 132 133 134 // 135 // ZJAutoCoding.m 136 // UI高級2 137 // 138 // Created by Jason_Msbaby on 15/10/13. 139 // Copyright ? 2015年 張杰. All rights reserved. 140 // 141 142 #import "ZJAutoCoding.h" 143 #import <objc/message.h> 144 #import <objc/runtime.h> 145 146 @implementation ZJAutoCoding 147 148 149 - (instancetype)initWithCoder:(NSCoder *)coder 150 { 151 self = [super init]; 152 if (self) { 153 //獲取所有屬性 154 unsigned int count = 0; 155 objc_property_t *pros = class_copyPropertyList(self.class, &count); 156 //遍歷屬性 157 for (int i = 0; i < count; i++) { 158 objc_property_t pro = pros[i]; 159 //當前屬性對應的名 160 NSString *proName = [NSString stringWithUTF8String:property_getName(pro)]; 161 unsigned int c = 0; 162 //獲取所有特征 163 objc_property_attribute_t *attrs = property_copyAttributeList(pro, &c); 164 //遍歷所有特征 165 for (int j = 0; j < c; j++ ) { 166 objc_property_attribute_t attr = attrs[j]; 167 //這里的V指的是知道屬性對應的成員變量的名稱 例如name屬性 在這里的特征值則為 V:_name 我們需要的是_name 168 if (strcmp(attr.name, "V") == 0) { 169 //然后我們需要獲取實例進行賦值操作 170 Ivar ivar = class_getInstanceVariable(self.class, attr.value); 171 object_setIvar(self, ivar,[coder decodeObjectForKey:proName]); 172 } 173 } 174 free(attrs); 175 } 176 free(pros); 177 178 } 179 return self; 180 } 181 182 -(void)encodeWithCoder:(NSCoder *)aCoder{ 183 //獲取所有的屬性 184 unsigned int count = 0; 185 objc_property_t *pros = class_copyPropertyList([self class], &count); 186 //遍歷屬性 187 for (int i = 0; i < count; i++) { 188 objc_property_t pro = pros[i]; 189 unsigned int c = 0; 190 //獲取實例變量的名稱 191 NSString *proName = [NSString stringWithUTF8String:property_getName(pro)]; 192 //通過當前的屬性 獲取該屬性的特征 193 objc_property_attribute_t *attrs = property_copyAttributeList(pro, &c); 194 //遍歷特征 能夠得到其類型以及數值等內容 195 for (int j = 0; j < c; j++) { 196 objc_property_attribute_t attr = attrs[j]; 197 if (strcmp(attr.name, "V") == 0) {//如果是特征的名稱 198 //獲取當前類對應特征名稱的實例變量 199 Ivar ivar = class_getInstanceVariable(self.class, attr.value); 200 //得到該實例變量的數值 201 id object = object_getIvar(self, ivar); 202 NSLog(@"%@",object); 203 //歸檔 204 [aCoder encodeObject:object forKey:proName]; 205 } 206 207 } 208 free(attrs); 209 } 210 free(pros); 211 } 212 213 @end?
轉載于:https://www.cnblogs.com/zhangjie9142/p/4887647.html
總結
以上是生活随笔為你收集整理的自动解析复杂类的属性 实现归档或者进行序列化 反序列话的时候为每一个属性添加序列化方法的繁琐...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [LeetCode]Palindrome
- 下一篇: JavaScript采用append添加