Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to save to a plist when application terminates
    text
    copied!<p>Ok so, I currently have a Master-Detail Application which has a tableview with a navigation controller, a detail view, and a modal view. The navigation view displays drink names, the detail view shows the selected drink details, and the modal view allows me to create a new drink or edit an existing one. Everything is populated from a plist which I already have moved to the documents directory and everything is reading from it just fine. I know this works because I have tested with an empty plist to see if my views change to blank, and they do successfully. All my views work perfectly. I can add edit and delete drinks and my table view updates with the new or deleted or changed drink. However it does not write to the plist file successfully (I am updating my plist from an array named "drinks" that is in my masterViewController). I think it is because I am trying to use this method below inside of my MasterViewController rather than in my AppDelegate.m</p> <pre><code>- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [searchPaths objectAtIndex:0]; NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DrinkDirections.plist"]; [self.drinks writeToFile:writeableDBPath atomically:YES]; } </code></pre> <p>I have tested this by placing 2 NSLogs(): one in the applicationWillTerminate method that <strong>I placed in</strong> my masterViewController and one in the applicationWillTerminate method that is in my AppDelegate.m. The log only shows in the console from the one in the AppDelegate.m. So finally I can't figure out how to access my drinks array that is in my MasterViewController from my AppDelegate.m file so I can use the applicationWillTerminate method that is in my appDelegate.m to save to the plist. I will post my appDelegate.m code below which throws an error because the drinks property is in the MasterViewController.</p> <p>AppDelegate.m (method)</p> <pre><code>- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [searchPaths objectAtIndex:0]; NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DrinkDirections.plist"]; [self.drinks writeToFile:writeableDBPath atomically:YES]; //THIS LINE THROWS AN ERROR BECAUSE PROPERTY DRINKS IS ONLY ON MY MASTERVIEWCONTROLLER NOT MY AppDelegate } </code></pre>
 

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