Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was just typing this up when Ulrich posted the correct answer. I'll add the rest here for extra clarification.</p> <p>As Ulrich pointed out, when you don't have a file, your code just skips the initialization of <code>favoriteJokesDictionary</code> and <code>favoriteJokes</code>. When you try to write out the object later, <code>favoriteJokesDictionary</code> is nil and so the method doesn't do anything.</p> <p>You can just create an empty NSMutableDictionary if the file doesn't exist yet, and since you're retaining part of the dictionary in a separate ivar as well you should create that at the same time. Something like this would work:</p> <pre><code>NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; self.favSaveFilePath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"Favorites.plist"]]; if([fm fileExistsAtPath:favSaveFilePath]) { favoriteJokesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:self.favSaveFilePath]; favoriteJokes = [[NSMutableArray alloc] initWithArray:[self.favoriteJokesDictionary objectForKey:@"FavoriteJokes"]]; } else { favoriteJokesDictionary = [[NSMutableDictionary alloc] init]; favoriteJokes = [[NSMutableArray alloc] init]; [favoriteJokesDictionary setObject:favoriteJokes forKey:@"JokesArrayKey"]; } </code></pre> <p>That said, I think you could probably simplify your data model there a bit (unless there much more going on that you're not showing). If you need a dictionary because you're storing more than just jokes, then what you have is fine. However, I wouldn't retain the jokes array, because if you later set a new object in the array for your JokesArrayKey, your favoriteJokes ivar is still going to be pointing to the old array.</p> <p>I would just grab the object from the dictionary instead whenever you need it:</p> <pre><code>NSMutableArray *jokes = [favoriteJokesDictionary objectForKey:@"JokesArrayKey"]; </code></pre> <p>Even better, if you are working with the jokes array quite a bit, just pull it out into its own file. You can write an NSArray out as a plist just like you can with an NSDictionary. If you're only using the NSDictionary as a shell to write out a .plist, you can skip it entirely.</p> <p>One other thing: Assuming your <code>favSaveFilePath</code> property is marked "retain", you have a small memory leak in the first section. You're creating a new retained string with <code>[[NSString alloc] initWithString:...]</code>, and then retaining it again by assigning it to your property. You probably just want:</p> <pre><code>self.favSaveFilePath = [documentsDirectory stringByAppendingPathComponent:@"Favorites.plist"]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload