开发者

iphone - Problem in writing NSDictionary to file

开发者 https://www.devze.com 2023-02-18 23:28 出处:网络
I\'m receiving json file from web server. I\'m using JSON files to parse (into arrays and dictionaries) and use the received information. But at point of time, i\'m trying to save a dictionary and its

I'm receiving json file from web server. I'm using JSON files to parse (into arrays and dictionaries) and use the received information. But at point of time, i'm trying to save a dictionary and its failing to write the file. The dict will have the following format:

{
    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";
}

I'm using the following code to save:

-(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;
}

I'm calling addJokeToFavorite with a dictionary. In addJokeToFavorite method, self.favoriteJokes is a property and it is retained also. The method savePlistFiles is always returning me success=NO. I'm not able to point out t开发者_StackOverflow中文版he reason why its not writing into file at all. I also tried creating the file first and then writing. It also didn't work. I also tried removing the keys whose value is , still no output.

I'm using the following code to create the plist file:

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


You should check self.favSaveFilePath and self.ratingSaveFilePath.

Could you give me more information about it ?

0

精彩评论

暂无评论...
验证码 换一张
取 消