为什么我不从NSKeyedUnarchiver获取数组?

时间:2017-07-31 09:30:08

标签: ios objective-c nskeyedarchiver

这是我的代码:

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 NSJSONSerialization to conver the NSdata to NSMutableArray and I got a nil`。任何人都知道哪里出错了?

这是我的cachePath:

#define kCacheFileName @"fakeLiveHistory.data"
#define kCachePath(kCacheFileName) ([NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:kCacheFileName])

1 个答案:

答案 0 :(得分:0)

你是ARCHIVE数据 - 下次收到数据时显然不是一个数组;)

将数组存档为开始

    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];

    [NSKeyedArchiver archiveRootObject:data toFile:path];

- >

    [NSKeyedArchiver archiveRootObject:arry toFile:path];