0

我正在从网络服务器接收 json 文件。我正在使用 JSON 文件来解析(到数组和字典中)并使用接收到的信息。但是在某个时间点,我正在尝试保存字典并且无法写入文件。dict 将具有以下格式:

{
    author = "<null>";
    category =     {
        "created_at" = "2011-02-06T18:11:39Z";
        id = 1;
        name = animals;
    };
    "created_at" = "<null>";
    id = 16;
    "mobile_user_id" = "<null>";
    "rating_count" = "4.33333333333333";
    status = 1;
    text = "A boy at a cinema notices what looks like a bear sitting next to him \"Are you a bear? \"\"Yes \"\"What are you doing at the movies? \"\"Well, I liked the book! \"";
    title = "Various animal jokes";
}

我正在使用以下代码进行保存:

-(BOOL)savePlistFiles:(int)fileNumber
{
    BOOL success = NO;
    switch (fileNumber) {
        case SAVE_FAVORITES_JOKES:
            success = [self.favoriteJokes writeToFile:self.favSaveFilePath atomically:YES];
            break;
        case SAVE_RATED_JOKES:
            success = [self.ratedJokes writeToFile:self.ratingSaveFilePath atomically:YES];
            break;
        default:
            NSLog(@"Improper file to save");
            success = NO;
            break;
    }
    return success;
}
-(BOOL)addJokeToFavorite:(NSDictionary*)joke
{
    NSLog(@"Joke: %@",joke);
    BOOL success = NO;
    [self.favoriteJokes addObject:joke];
    for(NSDictionary* dict in self.favoriteJokes) 
        NSLog(@"%@", dict);
    success = [self savePlistFiles:SAVE_FAVORITES_JOKES];
    return success;
}

我用字典调用 addJokeToFavorite 。在 addJokeToFavorite 方法中,self.favoriteJokes 是一个属性,它也被保留。方法 savePlistFiles 总是返回我成功=否。我无法指出它根本不写入文件的原因。我也尝试先创建文件然后再写入。它也没有工作。我还尝试删除值为 的键,但仍然没有输出。

我正在使用以下代码创建 plist 文件:

NSFileManager* fm = [NSFileManager defaultManager];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
self.favSaveFilePath = [documentsDirectory stringByAppendingPathComponent:@"Favorites.plist"];
4

1 回答 1

0

您应该检查 self.favSaveFilePath 和 self.ratingSaveFilePath。

你能给我更多关于它的信息吗?

于 2011-03-23T09:13:07.420 回答