存储缓存:第三方应用只能把信息保存在应用程序的沙盒中,因为缓存数据不是用户产生,所以应该将其保存在NSCahcesDictionary中而不是NSDocumentsDirectory中。因为把缓存存储在缓存文件夹下的原因是iCloud(和iTunes)的备份不包括此目录。如果放在了Documents目录下创建的大缓存将备份到iCloud的有限空间中。
* 序列化数据
* 以keyValue形式对基本数据类型Encoding
*/
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_title forKey:@"title"];
[aCoder encodeObject:_content forKey:@"content"];
[aCoder encodeObject:_modelId forKey:@"modelId"];
}
/**
* 反序列化数据
* 以keyValue形式对基本数据类型Decoding,返回数据模型本身
*/
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
NSLog(@"----> %s",__FUNCTION__);
self.title = [aDecoder decodeObjectForKey:@"title"];
self.content = [aDecoder decodeObjectForKey:@"content"];
self.modelId = [aDecoder decodeObjectForKey:@"modelId"];
}
return self;
}
for ( int i = 0; i < 10; i++) {
model= [[ZQModel alloc] init];
model.title = [NSString stringWithFormat:@"title%d",i];
model.content = [NSString stringWithFormat:@"content%d",i];
model.modelId = [NSString stringWithFormat:@"modelId%d",i];
[self.modelsArray addObject:model];
}
if ([NSKeyedArchiver archiveRootObject:self.modelsArray toFile:[[YCTools alloc] getCachePath:@"Appcache"]]) {
NSLog(@"<<<<< success >>>>");
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString * path = [[YCTools alloc] getCachePath:@"Appcache"];
cellArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
[self.tableView reloadData];
}