无法将我的自定义字典数据写入 plist 文件

Failed to write my custom dictionary data into plist file

我正在尝试使用以下自定义辅助方法将数据写入我的 plist 文件,该文件是在 Library/Caches 中在我的应用程序沙箱中创建的:

-(void)saveStatusesToPlist:(NSArray *)array
{
    NSDictionary *dict = @{@"data": array};

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDirectory = [paths objectAtIndex:0];

    NSString *plistPath = [cachesDirectory stringByAppendingPathComponent:filepath];
    NSLog(@"PATH: %@", plistPath);

    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:plistPath]) {
        BOOL isCreated = [manager createFileAtPath:plistPath contents:nil attributes:nil];
        NSLog(@"Result:%@", isCreated? @"Success": @"Failed");
    }
    BOOL flag = [dict writeToFile:plistPath atomically:YES];
    NSLog(@"Write result:%@", flag? @"Success": @"Failed");
}

请注意参数:array 是许多 NSDictionary 对象的数组,这些对象从 JSON 数据转换而来,这些数据是通过网络从服务器端获取的。

更新: 我的数组是这样的:

NSArray *array = [result objectForKey:@"array"];

但是结果总是失败

所以我怀疑问题出在 array 中,它可能包含无效数据。然后我尝试将 array 硬编码为 @[@"key1": @"value1", @"key2": @"value2", @"key3": @"value3"].

之类的东西

对于硬编码数据,flag returns YES 意味着它已被保存。

所以至少我可以说我获取的数据有问题,但我不知道它在哪里。

更新: 这是我的字典数据,非常大:

似乎问题出在字典的大小上…… 我创建了包含 30 个字典的数组,每个字典有 40 个项目。而且写不出来。。。 但!!! ;) 你可以做这样的事情:

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:dict]; 
BOOL flag = [data writeToFile:plistPath atomically:YES];

NSData* savedData = [NSData dataWithContentsOfFile:plistPath];
NSDictionary* unarchivedDict = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:savedData];

在这种情况下 - 一切正常。