6月4日 Object 保存到文件中
Q、 你添加一个新类到你的项目当中且你希望可以保存这个类的一个实例对象到磁盘文件 并在需要时从磁盘文件读回到内存中
A、 方案
确保你的类遵循 NSCoding 协议且在类中实现了需要是实现的方法.不要担心,在本节 的讨论部分我会带着你学习.
D、 讨论
在 IOS SDK 中有两个非常方便类来达到这个目的,在程序开发的术语中叫做编组,他们 是:
NSKeyedArchiver 一个利用键值来归档或存储对象或对象树的类.对象的每一个值,我们称为属性,
都能使用程序员选定的键值来归档.你将获得一个归档文件,让后你将可以保存你的
数值通过所选定顶的键值,此很像一个字典. NSKeyedUnarchiver
此类进行与归档类相反的操作.它能很简单地给你未归档的字典并要求你读取值到 属性中.
为了让归档及反归档工作正常,你需要确保需要归档及反归档的对象遵循 NSCoding 协 议.让我们以一个简单的 Person 类开始,如下是这个类的头文件:
@interface Person : NSObject<NSCoding>
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@end
如果现在你不写此类的任何实现代码且进行编译,你将会考到编译器抛出的警告,说你 没有遵循NSCoding协议且没有实现需要实现的方法.需要实现的方法如下:
- (void)encodeWithCoder:(NSCoder *)aCoder 此方法将给你一个编码器对象.此编码器对象你可以像一个词典那样使用,可以简单的通过你所选择的键值 存储数值进去.
- (id)initWithCoder:(NSCoder *)aDecoder; 当你使用NSKeyedUnarchiver对象反归档你的对象时,此方法将被调用.你可以很简单的通过传递进来的 NSCoder对象获取你回的数值
现在,基于上面的信息,我们实现我们的类:
#import "Person.h"
NSString *const kFirstNameKey = @"FirstNameKey";
NSString *const kLastNameKey = @"LastNameKey";
@implementation Person
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.firstName forKey:kFirstNameKey];
[aCoder encodeObject:self.lastName forKey:kLastNameKey];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self != nil) {
_firstName = [aDecoder decodeObjectForKey:kFirstNameKey];
_lastName = [aDecoder decodeObjectForKey:kLastNameKey];
}
return self;
}
@end
NSString *const kFirstName = @"Steven";
NSString *const kLastName = @"Jobs";
/* Determine where we want to archive the object */
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"steveJobs"];
/* Instantiate the object */
Person *steveJobs = [[Person alloc] init];
steveJobs.firstName = kFirstName;
steveJobs.lastName = kLastName;
/* Archive the object to the file */
NSData *data= [NSKeyedArchiver archivedDataWithRootObject:steveJobs];
[data writeToFile:filePath atomically:YES];
NSData *readData = [NSData dataWithContentsOfFile:filePath];
Person *cloneOfSteveJobs = [NSKeyedUnarchiver unarchiveObjectWithData:readData];
if ([cloneOfSteveJobs.firstName isEqualToString:kFirstName] &&
[cloneOfSteveJobs.lastName isEqualToString:kLastName]){
NSLog(@"Unarchiving worked"); } else {
NSLog(@"Could not read the same values back. Oh no!");
}
或者这样
/* Determine where we want to archive the object */
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"steveJobs.txt"];
/* Instantiate the object */
Person *steveJobs = [[Person alloc] init];
steveJobs.firstName = kFirstName;
steveJobs.lastName = kLastName;
/* Archive the object to the file */
[NSKeyedArchiver archiveRootObject:steveJobs toFile:filePath];
/* Now unarchive the same class into another object */
Person *cloneOfSteveJobs =
[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
/* Check if the unarchived object has the same first name and last name as the previously archived object */
if ([cloneOfSteveJobs.firstName isEqualToString:kFirstName] &&
[cloneOfSteveJobs.lastName isEqualToString:kLastName]){
NSLog(@"Unarchiving worked"); } else {
NSLog(@"Could not read the same values back. Oh no!");
}