前言
在iOS 中實現對象序列化,需要遵行NSCoding協議,然後對對象的每個屬性進行歸檔和接檔賦值,響應的操作比較繁瑣。本文主要介紹 利用 runtime遍歷屬性 大大簡化代碼量,下面來看看詳細的介紹吧。
具體實現代碼如下:
1.先建立NSobject的分類, 定義可能用到的相關類型
static NSString *intType = @"i"; // int_32t(枚舉int型) static NSString *longTpye = @"l"; //long類型 static NSString *longlongType= @"q"; // longlong類型 static NSString *BoolType = @"B"; //bool類型 static NSString *floatType = @"f"; // float static NSString *doubleType = @"d"; // double static NSString *boolType = @"c"; static NSString *stringType = @"NSString"; // NSString 類型 static NSString *numberType = @"NSNumber"; // NSNumber 類型 static NSString *arrayType = @"arrayType";//array類型 static NSString *imageType = @"UIImage"; // UIImage 類型
然後在歸檔方法中便利自身的屬性名稱,並且取出自身屬性對應的值,進行存儲到本地。此時遍歷類屬性本身,用到了Ivar指針(定義對象的實例變量,包括類型和名字),具體代碼如下
//歸檔 - (void)encodeWithCoder:(NSCoder *)aCoder { unsigned int count; // 屬性個數 Ivar *varArray = class_copyIvarList([self class], &count); for (int i = 0; i < count; i++) { Ivar var = varArray[i]; const char *cName = ivar_getName(var); // 屬性名c字符串 NSString *proName = [[NSString stringWithUTF8String:cName] substringFromIndex:1]; //OC字符串,並且去掉下劃線 _ const char *cType = ivar_getTypeEncoding(var); // 獲取變量類型,c字符串 id value = [self valueForKey:proName]; NSString *proType = [NSString stringWithUTF8String:cType]; // oc 字符串 if ([proType containsString:@"NSString"]) { proType = stringType; } if ([proType containsString:@"NSNumber"]) { proType = numberType; } if ([proType containsString:@"NSArray"]) { proType = arrayType; } if ([proType containsString:@"UIImage"]) { proType = imageType; } // (5). 根據類型進行編碼 if ([proType isEqualToString:intType] || [proType isEqualToString:boolType] || [proType isEqualToString:BoolType]) { [aCoder encodeInt32:[value intValue] forKey:proName]; } else if ([proType isEqualToString:longTpye]) { [aCoder encodeInt64:[value longValue] forKey:proName]; } else if ([proType isEqualToString:floatType]) { [aCoder encodeFloat:[value floatValue] forKey:proName]; } else if ([proType isEqualToString:longlongType] || [proType isEqualToString:doubleType]) { [aCoder encodeDouble:[value doubleValue] forKey:proName]; } else if ([proType isEqualToString:stringType]) { // string 類型 [aCoder encodeObject:value forKey:proName]; } else if ([proType isEqualToString:numberType]) { [aCoder encodeObject:value forKey:proName]; } else if ([proType isEqualToString:arrayType]) { [aCoder encodeObject:value forKey:proName]; } else if ([proType isEqualToString:imageType]) { // image 類型 [aCoder encodeDataObject:UIImagePNGRepresentation(value)]; } } free(varArray); }
其次進行解檔, 原理和歸檔差不多, 直接上代碼
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [self init]; if (self) { unsigned int count; Ivar *varArray = class_copyIvarList([self class], &count); for (int i = 0; i < count; i++) { Ivar var = varArray[i]; const char *cName = ivar_getName(var); // 屬性名c字符串 NSString *proName = [[NSString stringWithUTF8String:cName] substringFromIndex:1]; //OC字符串,並且去掉下劃線 _ const char *cType = ivar_getTypeEncoding(var); // 獲取變量類型,c字符串 NSString *proType = [NSString stringWithUTF8String:cType]; // oc 字符串 if ([proType containsString:@"NSString"]) { proType = stringType; } if ([proType containsString:@"NSNumber"]) { proType = numberType; } if ([proType containsString:@"NSArray"]) { proType = arrayType; } if ([proType containsString:@"UIImage"]) { proType = imageType; } if ([proType isEqualToString:intType] || [proType isEqualToString:boolType] || [proType isEqualToString:BoolType]) { int32_t number = [aDecoder decodeInt32ForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:longTpye]) { int64_t number = [aDecoder decodeInt64ForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:floatType]) { float number = [aDecoder decodeFloatForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:longlongType] || [proType isEqualToString:doubleType]) { double number = [aDecoder decodeFloatForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:stringType]) { // string 類型 NSString *string = [aDecoder decodeObjectForKey:proName]; [self setValue:string forKey:proName]; } else if ([proType isEqualToString:numberType]) { NSString *number = [aDecoder decodeObjectForKey:proName]; [self setValue:number forKey:proName]; } else if ([proType isEqualToString:arrayType]) { NSArray *array = [aDecoder decodeObjectForKey:proName]; [self setValue:array forKey:proName]; } else if ([proType isEqualToString:imageType]) { // image 類型 UIImage *image = [UIImage imageWithData:[aDecoder decodeDataObject]]; [self setValue:image forKey:proName]; } } } return self; }
最後也就是 存儲方法 、 清除存儲的本地緩存 和 獲取本地存儲數據的方法
//存儲路徑 - (NSString *)filePathWithUniqueFlagString:(NSString *)uniqueFlag { NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *detailPath = [NSString stringWithFormat:@"%@_%@",uniqueFlag,[NSString stringWithUTF8String:object_getClassName(self)]]; NSString *path = [docPath stringByAppendingPathComponent:detailPath]; return path; } //保存對象數據到本地 - (void)saveDataToLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { [NSKeyedArchiver archiveRootObject:self toFile:[self filePathWithUniqueFlagString:uniqueFlagKey]]; } //清空本地存儲的對象數據 - (id)getDataFromLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { return [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePathWithUniqueFlagString:uniqueFlagKey]]; } //從本地獲取對象數據 - (BOOL)removeDataFromLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { NSError *error = nil; [[NSFileManager defaultManager] removeItemAtPath:[self filePathWithUniqueFlagString:uniqueFlagKey] error:&error]; if (!error) { return YES; } else { return NO; } }
完整項目下載地址如下:https://github.com/maxzhang123/MXCoding.git 或者可以本地下載地址:http://xiazai.jb51.net/201705/yuanma/MXCoding(jb51.net).rar
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對本站的支持。