title: Why isnt my iOS data persisting with NSKeyedArchiver?
body: <p>Im just working on what should be the "finishing touches" of my first iPhone game. For some reason, when I save with NSKeyedArchiver/Unarchiver, the data seems to load once and then gets lost or something. </p>
  
  <p>I'm trying to save 2 objects with the same archiver: an <code>NSMutableDictionary levelsPlist</code>, and an <code>NSMutableArray categoryLockStateArray</code>. The are set as nonatomic, retain properties in the header file.</p>
  
  <p>Here's what I've been able to deduce:</p>
  
  <ol>
  <li><p>The object saved as the NSMutableDictionary always persists. It works just fine.</p></li>
  <li><p>When I save in this viewController, <strong>pop</strong> to the previous one, and then <strong>push</strong> back into this one, the data is saved and prints as I want it to.</p></li>
  <li><p>But when I save in this viewController, then <strong>push</strong> a new one and <strong>pop</strong> back into this one, the categoryLockState is lost.</p></li>
  </ol>
  
  <p>Any idea why this might be happening? Do I have this set up all wrong? I copied it from a book months ago. Here's the methods I use to save and load. </p>
  
  <pre><code>    - (void) saveGameData {
          NSLog(@"LS:saveGameData");
  
          // SAVE DATA IMMEDIATELY
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
          NSMutableData *gameSave= [NSMutableData data];
          NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameSave];
          [encoder encodeObject:self.categoryLockStateArray forKey:@"categoryLockStateArray];
          [encoder encodeObject:self.levelsPlist forKey:@"levelsPlist"];
  
          [encoder finishEncoding];
          [gameSave writeToFile:gameStatePath atomically:YES];
  
          NSLog(@"encoded catLockState:%@",categoryLockStateArray);
      }
  
  - (void) loadGameData {
      NSLog(@"loadGameData");
  
      // If there is a saved file, perform the load
      NSMutableData *gameData = [NSData dataWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"gameState.dat"]];
  
      // LOAD GAME DATA
      if (gameData) {
          NSLog(@"-Loaded Game Data-");
          NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
          self.levelsPlist = [unarchiver decodeObjectForKey:@"levelsPlist"];
          categoryLockStateArray = [unarchiver decodeObjectForKey:@"categoryLockStateArray"];
  
          NSLog(@"decoded catLockState:%@",categoryLockStateArray);
      }
  
      // CREATE GAME DATA
      else {
          NSLog(@"-Created Game Data-");
          self.levelsPlist = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLevelsPlist ofType:@"plist"]];
      }
  
      if (!categoryLockStateArray) {
          NSLog(@"-Created categoryLockStateArray-");
  
          categoryLockStateArray = [[NSMutableArray alloc] initWithCapacity:[[self.levelsPlist allKeys] count]];
  
          for (int i=0; i&lt;[[self.levelsPlist allKeys] count]; i++) {
              [categoryLockStateArray insertObject:[NSNumber numberWithBool:FALSE] atIndex:i];
          }
      }
  
      // set the properties of the categories
      self.categoryNames = [self.levelsPlist allKeys];
      NUM_CATEGORIES = [self.categoryNames count];
      thisCatCopy = [[NSMutableDictionary alloc] initWithDictionary:[[levelsPlist objectForKey:[self.categoryNames objectAtIndex:pageControl.currentPage]] mutableCopy]];
      NUM_FINISHED = [[thisCatCopy objectForKey:kNumLevelsBeatenInCategory] intValue];
  }
  </code></pre>
  
  <p>I can only guess that it is related to the fact that the viewController is unloaded and reloaded when you pop then push back in, but my viewDidLoad code makes no mention of either of these variables. They are called from the viewDidAppear method.</p>
  
  <p>Thanks for any help you can offer!</p>