iOS 归档解档
Posted tongyuling
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 归档解档相关的知识,希望对你有一定的参考价值。
归档:
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];
以上是关于iOS 归档解档的主要内容,如果未能解决你的问题,请参考以下文章
iOS 数据的归档(NSKeyedArchiver )与数据的解档(NSKeyedUnarchiver) 加密 提高安全性