【发布时间】:2017-07-31 09:30:08
【问题描述】:
这是我的代码:
NSString * path = kCachePath(kCacheFileName);
NSMutableArray * arry = [[NSMutableArray alloc]init];
arry = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSData * data = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSError *error = nil;
// arry = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
if (arry) {
[arry addObject:playModel];
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];
[NSKeyedArchiver archiveRootObject:data toFile:path];
return YES;
} else {
NSMutableArray * ary = [NSMutableArray array];
[ary addObject:playModel];
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:ary];
[NSKeyedArchiver archiveRootObject:data toFile:path];
}
我已经成功地将一个数组存档到文件中,但是当我想解开数据文件时,我得到了一个NSMutableData', But I want anNSMutableArray. and I have tried theNSJSONSerializationto conver theNSdatatoNSMutableArrayand I got anil`。有谁知道哪里出错了?
这是我的缓存路径:
#define kCacheFileName @"fakeLiveHistory.data"
#define kCachePath(kCacheFileName) ([NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:kCacheFileName])
【问题讨论】:
标签: ios objective-c nskeyedarchiver