這篇文章將為大家詳細(xì)講解有關(guān)iOS中如何自動實(shí)現(xiàn)對象序列化,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)科技有限公司專業(yè)互聯(lián)網(wǎng)基礎(chǔ)服務(wù)商,為您提供服務(wù)器托管機(jī)柜,高防服務(wù)器,成都IDC機(jī)房托管,成都主機(jī)托管等互聯(lián)網(wǎng)服務(wù)。具體實(shí)現(xiàn)代碼如下:
1.先建立NSobject的分類, 定義可能用到的相關(guān)類型
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 類型
然后在歸檔方法中便利自身的屬性名稱,并且取出自身屬性對應(yīng)的值,進(jìn)行存儲到本地。此時(shí)遍歷類屬性本身,用到了Ivar指針(定義對象的實(shí)例變量,包括類型和名字),具體代碼如下
//歸檔 - (void)encodeWithCoder:(NSCoder *)aCoder { unsigned int count; // 屬性個(gè)數(shù) 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). 根據(jù)類型進(jìn)行編碼 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); }
其次進(jìn)行解檔, 原理和歸檔差不多, 直接上代碼
- (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; }
最后也就是 存儲方法 、 清除存儲的本地緩存 和 獲取本地存儲數(shù)據(jù)的方法
//存儲路徑 - (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; } //保存對象數(shù)據(jù)到本地 - (void)saveDataToLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { [NSKeyedArchiver archiveRootObject:self toFile:[self filePathWithUniqueFlagString:uniqueFlagKey]]; } //清空本地存儲的對象數(shù)據(jù) - (id)getDataFromLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { return [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePathWithUniqueFlagString:uniqueFlagKey]]; } //從本地獲取對象數(shù)據(jù) - (BOOL)removeDataFromLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { NSError *error = nil; [[NSFileManager defaultManager] removeItemAtPath:[self filePathWithUniqueFlagString:uniqueFlagKey] error:&error]; if (!error) { return YES; } else { return NO; } }
關(guān)于“iOS中如何自動實(shí)現(xiàn)對象序列化”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站m.newbst.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文題目:iOS中如何自動實(shí)現(xiàn)對象序列化-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://m.newbst.com/article46/dpgoeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站改版、用戶體驗(yàn)、關(guān)鍵詞優(yōu)化、軟件開發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容