標籤:
使用NSKeyedArichiver進行歸檔、NSKeyedUnarchiver進行接檔,這種方式會在寫入、讀出資料之前對資料進行序列化、還原序列化操作。
1.對單個字串歸檔
//擷取根目錄 NSString *homeDictionary = NSHomeDirectory(); //添加儲存的檔案名稱 NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"myText.txt"]; //歸檔一個字串---返回是否成功的標誌 BOOL flag = [NSKeyedArchiver archiveRootObject:@"歸檔字串內容..." toFile:homePath]; if (flag) { NSLog(@"歸檔成功!!!"); //將字串從歸檔內容中解檔出來 NSString *str = [NSKeyedUnarchiver unarchiveObjectWithFile:homePath]; NSLog(@"%@",str); }else { NSLog(@"歸檔失敗!!!"); }
/*對當個的數組或者字典歸檔的時候 他們內部包含的對象必須都實現了NSCoding協議才能歸檔成功*/
2.對多種不同類型的資料同時歸檔
//一個CGPoint點、字串、整數(當然很多類型都可以的,例如UIImage、float等等) //準備資料 CGPoint point = CGPointMake(1.0, 2.0); NSString *string = @"座標原點"; NSInteger number = 10; NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"yyehdh.archiver"]; NSMutableData *data = [[NSMutableData alloc]init]; NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; //對多個對象進行歸檔 [archvier encodeCGPoint:point forKey:@"kPoint"]; [archvier encodeObject:string forKey:@"kInfo"]; [archvier encodeInteger:number forKey:@"kValue"]; //調用歸檔結束關閉函數 [archvier finishEncoding]; //將資料歸檔--返迴歸檔成功與否的標誌 BOOL flag = [data writeToFile:multiHomePath atomically:YES]; if (flag) { NSLog(@"資料歸檔成功!!!!"); //從剛才歸檔的檔案將將資料讀取出來 NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:dataR]; //對資料進行解檔 得到相應的資料類型的資料(和歸檔的時候相同的資料類型) CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"]; NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"]; NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"]; //調用解檔結束關閉函數 [unarchiver finishDecoding]; NSLog(@"%f,%f,%@,%ld",pointR.x,pointR.y,infoR,(long)valueR); }else { NSLog(@"歸檔失敗!!!!"); }
3.自訂對象歸檔
操作步驟
歸檔:
1.使用NSData執行個體作為歸檔的儲存資料
2.添加歸檔的內容---使用索引值對
3.完成歸檔
解歸檔:
1.從磁碟讀取檔案,產生NSData執行個體
2.根據NSData執行個體和初始化解歸檔執行個體
3.解歸檔,根據key訪問value
//// A.h// 歸檔解檔//// Created by admin on 16/1/18.// Copyright © 2016年 123. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface A : NSObject<NSCoding>-(instancetype)initWithName:(NSString *)name icon:(UIImage *)anImage age:(NSInteger)age;@property(nonatomic,copy)NSString *name;@property(nonatomic,assign)NSInteger age;@property(nonatomic,copy)UIImage *icon;@end
//// A.m// 歸檔解檔//// Created by admin on 16/1/18.// Copyright © 2016年 123. All rights reserved.//#import "A.h"#define kNameKey @"NameKey"#define kAgeKey @"AgeKey"#define KIconKey @"IconKey"@implementation A-(instancetype)initWithName:(NSString *)name icon:(UIImage *)anImage age:(NSInteger)age;{ if (self = [super init]) { self.name = name; self.age = age; self.icon = anImage; } return self;}/* 【注】:要將一個自訂的類進行歸檔,那麼類裡面的每個屬性都必須是可以被歸檔的,如果是不能歸檔的類型,我們可以把他轉化為NSValue進行歸檔,然後在讀出來的時候在轉化為相應的類。 */#pragma mark - 通過一個給定的archiver把訊息接收者進行編碼-(void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:_name forKey:kNameKey]; [aCoder encodeInteger:_age forKey:kAgeKey]; [aCoder encodeObject:_icon forKey:KIconKey];}#pragma mark - 從一個給定unarchiver的資料中返回一個初始化對象。- (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { _name = [aDecoder decodeObjectForKey:kNameKey]; _age = [aDecoder decodeIntegerForKey:kAgeKey]; _icon = [aDecoder decodeObjectForKey:KIconKey]; } return self;}#pragma mark - 返回訊息接收者的一個複製的新執行個體。- (id)copyWithZone:(NSZone *)zone{ A *copy = [[[self class] allocWithZone:zone] init]; copy.name = [self.name copyWithZone:zone]; copy.age = self.age; copy.icon = self.icon ; return copy;}@end
NSString *multiHomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"ggdh.archiver"]; A *aPerson = [[A alloc]initWithName:@"zhangsan" icon:[UIImage imageNamed:@"oicon"] age:88]; NSLog(@"%@",[UIImage imageNamed:@"oicon"]); //歸檔 NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //A類的 encodeWithCoder 將會被調用 [archiver encodeObject:aPerson forKey:@"ObjArchiverKey"]; [archiver finishEncoding]; //寫入檔案 BOOL flag = [data writeToFile:multiHomePath atomically:YES]; if (flag) { NSLog(@"自訂對象歸檔成功!!!"); NSData *data = [[NSMutableData alloc] initWithContentsOfFile:multiHomePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //獲得類--解檔自訂的對象 A *readPerson = [unarchiver decodeObjectForKey:@"ObjArchiverKey"];// initWithCoder方法被調用 [unarchiver finishDecoding]; NSLog(@"name:%@ ~~ age:%ld ~~ icon: %@",readPerson.name,readPerson.age,readPerson.icon); } else { NSLog(@"自訂對象歸檔失敗!!!"); }
iOS歸檔解檔