我想缓存一些 https 请求的结果以加速用户界面,所以我想在 applicationWillResignActive: 中缓存数据并在 didFinishLaunchingWithOptions 中恢复它。为此,我使用以下代码片段: NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachesDirectory=[paths objectAtIndex:0];
NSString* archivePath=[cachesDirectory stringByAppendingPathComponent:@"AppCache/Paline.archive"];
NSLog(@"archivePath=%@ paline=%@", archivePath, detailViewController.palineArray);
BOOL success=[NSKeyedArchiver archiveRootObject:detailViewController.palineArray toFile:archivePath];
NSLog(@"%@",success?@"success": @"failure");
失败并在恢复时返回 nil:
NSMutableArray* cachedItems=[NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
NSLog(@"archivePath=%@ cache=%@", archivePath, cachedItems);
palineArray 是一个带有值的 NSMutableArray,当序列化时,如前面的 NSLog 所示。会是什么呢?它不符合 NSMutableArray 编码还是什么?
谢谢,法布里齐奥