对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化)
主要涉及两个类:NSKeyedArichiver、NSKeyedUnarchiver
两个协议NSCoding的2个方法:
- (void)encodeWithCoder:
- (id)initWithCoder:
可以接着(一)的例子,新建文件,subclass of NSObject, 实现NSCoding协议
新建文件.h
#import <Foundation/Foundation.h> @interface BSLFourLines : NSObject<NSCoding> @property (copy, nonatomic) NSString *field1; @property (copy, nonatomic) NSString *field2; @property (copy, nonatomic) NSString *field3; @property (copy, nonatomic) NSString *field4; @end
新建文件.m
#import "BSLFourLines.h" #define kField1Key @"Field1" #define kField2Key @"Field2" #define kField3Key @"Field3" #define kField4Key @"Field4" @implementation BSLFourLines @synthesize field1,field2,field3,field4; #pragma mark NSCoding //以keyValue形式对基本数据类型Encoding - (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:field1 forKey:kField1Key]; [aCoder encodeObject:field2 forKey:kField2Key]; [aCoder encodeObject:field3 forKey:kField3Key]; [aCoder encodeObject:field4 forKey:kField4Key]; } //以keyValue形式对基本数据类型Decoding,返回数据模型本身 - (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { field1 = [aDecoder decodeObjectForKey:kField1Key]; field2 = [aDecoder decodeObjectForKey:kField2Key]; field3 = [aDecoder decodeObjectForKey:kField3Key]; field4 = [aDecoder decodeObjectForKey:kField4Key]; } return self; }
现在转到XXViewController.m在#import下方添加
#import "BSLFourLines.h"
#define kFilename2 @"archive"
#define kDataKey @"Data"
改动后的方法内容
- (void)viewDidLoad { [super viewDidLoad]; NSString *filePath = [self dataFilePath]; //检查数据文件是否存在,如果存在就用此文件内容实例化数组 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; BSLFourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey]; [unarchiver finishDecoding]; field1.text = fourLines.field1; field2.text = fourLines.field2; field3.text = fourLines.field3; field4.text = fourLines.field4; } //通知 UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; }
- (void)applicationWillResignActive:(NSNotification *)notification{ BSLFourLines *fourLines = [[BSLFourLines alloc] init]; fourLines.field1 = field1.text; fourLines.field2 = field2.text; fourLines.field3 = field3.text; fourLines.field4 = field4.text; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:fourLines forKey:kDataKey]; [archiver finishEncoding]; [data writeToFile:[self dataFilePath] atomically:YES]; }
如果需要对自定义的类进行copy的话,需要在自定义类实现<NSCoding>协议
自定义类的实现文件里添加下列方法
- (id)copyWithZone:(NSZone *)zone{ BSLFourLines *copy = [[[self class] allocWithZone:zone] init]; copy.field1 = [self.field1 copyWithZone:zone]; copy.field2 = [self.field2 copyWithZone:zone]; copy.field3 = [self.field3 copyWithZone:zone]; copy.field4 = [self.field4 copyWithZone:zone]; return copy; }