歸檔是指一種形式的序列化,專門編寫用於保存數據的任何對象都應該支持歸檔。使用對模型對象進行歸檔的技術可以輕松將復雜的對象寫入文件,然後再從中讀取它們。
只要在類中實現的每個屬性都是標量或者都是遵循NSCoding協議的某個類的實例,你就可以對整個對象進行完全歸檔。大多數的Foundation和Cocoa Touch類 都遵NSCoding協議,所以對於有大多數類來說,歸檔不太難。
遵循NSCoding協議:
NSCoding聲明了兩個方法,
- (void)encodeWithCoder:(NSCoder *)aCoder;和- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;
第一個是 將對象編碼到歸檔中,第二個則是對歸檔解碼,來創建一個對象。
在BIDFourLines中:
#import <Foundation/Foundation.h> @interface BIDFourLines : NSObject <NSCoding, NSCopying> @property (copy, nonatomic) NSArray *lines; @end
#import "BIDFourLines.h" static NSString * const kLinesKey = @"kLinesKey"; @implementation BIDFourLines #pragma mark - Coding - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { self.lines = [aDecoder decodeObjectForKey:kLinesKey]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder; { [aCoder encodeObject:self.lines forKey:kLinesKey]; } #pragma mark - Copying - (id)copyWithZone:(NSZone *)zone; { BIDFourLines *copy = [[[self class] allocWithZone:zone] init]; NSMutableArray *linesCopy = [NSMutableArray array]; for (id line in self.lines) { [linesCopy addObject:[line copyWithZone:zone]]; } copy.lines = linesCopy; return copy; } @end
注:上面遵循了NSCopying協議,遵循NSCopying協議對於任何模型對象來說都是非常好的事情,NSCopying協議中,有一個copyWithZone方法,可以用來復制對象。
在BIDViewController中:
#import "BIDViewController.h" #import "BIDFourLines.h" static NSString * const kRootKey = @"kRootKey"; @interface BIDViewController () @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *lineFields; @end @implementation BIDViewController - (NSString *)dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:@"data.archive"]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString *filePath = [self dataFilePath]; //如果存在歸檔 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //解檔 BIDFourLines *fourLines = [unarchiver decodeObjectForKey:kRootKey]; [unarchiver finishDecoding]; for (int i = 0; i < 4; i++) { UITextField *theField = self.lineFields[i]; theField.text = fourLines.lines[i]; } } //後台通知 UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; } //進入後台時 歸檔 - (void)applicationWillResignActive:(NSNotification *)notification { NSString *filePath = [self dataFilePath]; BIDFourLines *fourLines = [[BIDFourLines alloc] init]; fourLines.lines = [self.lineFields valueForKey:@"text"]; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:fourLines forKey:kRootKey]; [archiver finishEncoding]; [data writeToFile:filePath atomically:YES]; }