0

我有一个 .net 网络服务,它以这种格式返回 Json 数据:

        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataPlist options:NSJSONReadingMutableContainers error:&errorJson];

[
{
    "MEMBERID": "xxx",
    "PREFIXINFORMAL": "Mr.",
    "FIRSTNAME": "xxx",
    "MIDDLENAME": "xxxx",
    "LASTNAME": "xxxx",
    "SUFFIX": xxxx
}

]

我将此 NSDictionary 保存到 plist 的代码:

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"boardMemberInfo.plist"];
        NSDictionary *test = [NSDictionary dictionaryWithObject:@"test" forKey:@"test"];
        for(NSDictionary *item in json) {
            BOOL success = [item writeToFile:path atomically:YES];
            if(success) {
                NSLog(@"Success Saving New plist");
            } else {
                NSLog(@"Failure Saving New plist");
            }
        }

[item writeToFile...] 和 [json writeToFile...] 都不起作用。但是,简单的 [test writeToFile...] 有效。我错过了什么?

谢谢,

4

1 回答 1

0

您可以做的最好的事情是分解您的保存过程并使用 NSError 来了解问题所在:

    for(NSDictionary *item in json) {
        NSError *error;
        NSData *serializedData = [NSPropertyListSerialization dataWithPropertyList:item format:… options:… error:&error];
        if (!serializedData) { NSLog(@"Can't serialize data: %@", error); continue; }
        if (![serializedData writeToFile:path options:… error:&error]) {
            NSLog(@"Can't write to %@: %@", path, error);
        } else {
            NSLog(@"Success Saving New plist");
        }
    }
于 2012-08-16T18:32:38.967 回答