iphone - 将NSDictionary写入文件时出现问题

时间:2011-03-23 09:10:00

标签: iphone nsdictionary file-writing

我正在从网络服务器接收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方法总是返回成功= NO。我无法指出它根本没有写入文件的原因。 我还尝试先创建文件,然后再编写。它也没用。 我也尝试删除其值仍然没有输出的键。

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

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

1 个答案:

答案 0 :(得分:0)

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

你能告诉我更多有关它的信息吗?