2017-07-31 26 views
0

这里是我的代码:为什么我不从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 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]) 

回答

0

您归档数据 - 而不是一个数组所以很明显下一次你找回数据;)

归档阵列作为启动

NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry]; 

    [NSKeyedArchiver archiveRootObject:data toFile:path]; 

- >

[NSKeyedArchiver archiveRootObject:arry toFile:path]; 
相关问题