【问题标题】:Why don't I get an Array from a NSKeyedUnarchiver?为什么我没有从 NSKeyedUnarchiver 获得一个数组?
【发布时间】: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


    【解决方案1】:

    你存档数据 - 不是一个数组,所以下次你取回数据时很明显;)

    将数组存档作为开始

        NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];
    
        [NSKeyedArchiver archiveRootObject:data toFile:path];
    

    -->

        [NSKeyedArchiver archiveRootObject:arry toFile:path];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2011-10-24
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多