为什么我不能从 NSKeyedUnarchiver 获取数组?
Why don't I get an Array from a NSKeyedUnarchiver?
这是我的代码:
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 an
NSMutableArray. and I have tried the
NSJSONSerializationto conver the
NSdatato
NSMutableArrayand I got a
无`。有人知道哪里出了问题吗?
这是我的缓存路径:
#define kCacheFileName @"fakeLiveHistory.data"
#define kCachePath(kCacheFileName) ([NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:kCacheFileName])
您归档数据 - 显然不是数组,所以下次您取回数据时 ;)
将数组存档作为开始
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];
[NSKeyedArchiver archiveRootObject:data toFile:path];
-->
[NSKeyedArchiver archiveRootObject:arry toFile:path];
这是我的代码:
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 an
NSMutableArray. and I have tried the
NSJSONSerializationto conver the
NSdatato
NSMutableArrayand I got a
无`。有人知道哪里出了问题吗?
这是我的缓存路径:
#define kCacheFileName @"fakeLiveHistory.data"
#define kCachePath(kCacheFileName) ([NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:kCacheFileName])
您归档数据 - 显然不是数组,所以下次您取回数据时 ;)
将数组存档作为开始
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];
[NSKeyedArchiver archiveRootObject:data toFile:path];
-->
[NSKeyedArchiver archiveRootObject:arry toFile:path];