标签:
归档:
NSMutableData *data = [[NSMutableData alloc] init];
//创建归档辅助类
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//编码
[archiver encodeObject:@"2009-12-09" forKey:@"dataTime"];
//结束编码
[archiver finishEncoding];
//写入
[data writeToFile:[self getFilePathWithModelKey:[NSString stringWithFormat:@"test"]] atomically:YES];
方法://得到Document目录
-(NSString *) getFilePathWithModelKey:(NSString *)modelkey
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[array objectAtIndex:0] stringByAppendingPathComponent:modelkey];
}
解档:
NSData * data = [[NSData alloc] initWithContentsOfFile:[self getFilePathWithModelKey:[NSString stringWithFormat:@"test"]]];
//解档辅助类
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSString * time = [unarchiver decodeObjectForKey:@"dataTime"];
//关闭解档
[unarchiver finishDecoding];
标签:
原文地址:http://www.cnblogs.com/tongyuling/p/5620881.html