您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關iOS中如何自動實現對象序列化,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體實現代碼如下:
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; } }
關于“iOS中如何自動實現對象序列化”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。