Search code examples
iosobjective-cdictionarynsdictionaryplist

Adding Multiple Dictionaries to iOS plist for Journal


I have an app in which I want to store several pieces of information throughout the life of the app. To do this, I have made a plist file called 'myJournal.plist'. There won't be anything in the plist at first, as it will be completely dependent on what the user does. I would like the plist to look something like this, where each Dictionary is based on the current day and added to the list.

enter image description here

I have the following in my code:

NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [directories firstObject];
    NSString *filename = [documents stringByAppendingPathComponent:@"myJournal.plist"];
    NSMutableDictionary *loadedMiscDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];

    [tempDict setValue:distractions.text forKey:@"distractions"];
    [tempDict setValue:revelations.text forKey:@"revelations"];

    [loadedMiscDictionary addEntriesFromDictionary:tempDict];
    [loadedMiscDictionary writeToFile:filename atomically:YES];

But nothing gets changed. What am I doing wrong?


Solution

    1. First time you may be trying to write into file which is either not created yet or empty thus your loadedMiscDictionary is nil. As a result, at the end you're trying to write nil into file.

    2. The file in the bundle (which is on the screenshot, I assume) is not the same file that resides in Documents directory. Enable documents sharing and check your app's Documents directory in iTunes. I think you'll see the file that is actually altered there.