title: How to save data from multiple views of an iPhone app?
body: <p>I'm making an app where I need to save the text in multiple views in the app when the app quits. I also need to be able to remove all of the data from just one of those views and when the app quits, it's possible not all of those views will have been created yet.</p>
  
  <p>After reading <a href="https://stackoverflow.com/questions/338734/iphone-proper-usage-of-application-delegate/338832#338832">this post</a> I thought perhaps it would be good to use a singleton that manages my app data which loads in the data when it is first requested and saved it when the app quits. Then in each view where I need to save data I can just set it on the singleton.</p>
  
  <p>I gave it a go but have run into some issues. At first I didn't synthesize the properties (as in the post I was using as a guide) but the compiler told me I needed to make getters and setters, so I did. Now when my applicationWIllTerminate: gets call the app crashes and the console says "Program received signal:  “EXC_BAD_ACCESS”. kill quit".</p>
  
  <p>Is anyone able to tell me what I'm doing wrong, or suggest a better approach to saving the data?</p>
  
  <p>//SavedData.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  #define kFileName @"appData.plist"
  
  @interface SavedData : NSObject {
      NSString *information;
      NSString *name;
      NSString *email;
      NSString *phone;
      NSString *mobile;
  }
  
  @property(assign) NSString *information;
  @property(assign) NSString *name;
  @property(assign) NSString *email;
  @property(assign) NSString *phone;
  @property(assign) NSString *mobile;
  
  + (SavedData *)singleton;
  
  + (NSString *)dataFilePath;
  + (void)applicationWillTerminate:(NSNotification *)notification;
  @end
  </code></pre>
  
  <p>//SavedData.m</p>
  
  <pre><code>#import "SavedData.h"
  
  @implementation SavedData
  
  @synthesize information;
  @synthesize name;
  @synthesize email;
  @synthesize phone;
  @synthesize mobile;
  
  static SavedData * SavedData_Singleton = nil;
  
  + (SavedData *)singleton{
  
      if (nil == SavedData_Singleton){
          SavedData_Singleton = [[SavedData_Singleton alloc] init];
  
          NSString *filePath = [self dataFilePath];
          if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
              NSMutableArray * array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
              information = [array objectAtIndex:0];
              name = [array objectAtIndex:1];
              email = [array objectAtIndex:2];
              phone = [array objectAtIndex:3];
              mobile = [array objectAtIndex:4];           
              [array release];
          }
          UIApplication *app = [UIApplication sharedApplication];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app]; 
      }
      return SavedData_Singleton;
  }
  
  + (NSString *)dataFilePath{
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *DocumentsDirectory = [paths objectAtIndex:0];
      return [DocumentsDirectory stringByAppendingPathComponent:kFileName];
  }
  
  + (void)applicationWillTerminate:(NSNotification *)notification{
      NSLog(@"Application will terminate received");
      NSMutableArray *array = [[NSMutableArray alloc] init];
      [array addObject:information];
      [array addObject:name];
      [array addObject:email];
      [array addObject:phone];
      [array addObject:mobile];
      [array writeToFile:[self dataFilePath] atomically:YES];
      [array release];
  }
  
  @end
  </code></pre>
  
  <p>Then when I want to use it I do</p>
  
  <pre><code>myLabel.text = [SavedData singleton].information;
  </code></pre>
  
  <p>And when I change the field</p>
  
  <pre><code>[SavedData singleton].information = @"my string";
  </code></pre>
  
  <p>Any help will be very much appreciated!</p>