title: Updating and saving data in plist
body: <p>I am making an iOS game and need to save the highest level the player has reached. I can successfully change a data element in a plist, but for some reason, that data keeps reverting to its original value every time the game restarts. Here is the basic flow of my code:</p>
  
  <p>In the game's init, get the highest level the player has reached (the original value is 1)</p>
  
  <pre><code>pData = [[PlayerData alloc] init];
  currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
  [self startNewLevel:currentLevel];
  </code></pre>
  
  <p>'data' is an NSMutableDictionary that gets initialized like this in PlayerData:</p>
  
  <pre><code>self.data = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"]];
  </code></pre>
  
  <p>later, if the player beats the highest level, I increment the 'highest level' value and write to the file by calling this function in PlayerData:</p>
  
  <pre><code>-(void) newHighestLevel:(NSString*)path :(int)level{
       [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
       [self.data writeToFile:@"playerdata.plist" atomically:YES]
  </code></pre>
  
  <p>I know all this is working because I have a level menu that the player can access while playing the game. Each time the player presses the level menu button, a subclass of a UITableView gets created that displays level 1 through the highest level reached. The initialization looks like this:</p>
  
  <pre><code>levelMenu = [[LevelMenu alloc] init:[[pData.data valueForKey:@"Highest Level"] intValue]];
  </code></pre>
  
  <p>The level menu displays the correct number of levels while in the game (e.g. if the player hasn't beat any levels and goes to the level menu, it just displays "level 1"; but if the player beats level 1 and goes to the level menu, it displays "level 1" and "level 2"). However, whenever the app gets terminated or the user quits to the games main menu, the value for 'highest level' reverts back to 1 and this code: </p>
  
  <pre><code>currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
  </code></pre>
  
  <p>always sets currentLevel to 1 when the player pushes start, no matter how many levels the user beat while playing.</p>
  
  <p>Why is the value changing back? Am I missing something that would make my plist edits permanent?</p>
  
  <p>EDIT:</p>
  
  <p>here is my new 'newHighestLevel' method:</p>
  
  <pre><code>-(void) newHighestLevel:(NSString*)path :(int)level{
      [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *basePath = ([paths count] &gt; 0) ? [paths objectAtIndex:0] : nil;
      NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
      [self.data writeToFile:docfilePath atomically:YES];
      self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
      BOOL write = [self.data writeToFile:docfilePath atomically:YES];
  }
  </code></pre>
  
  <p>write gets set to YES. If I change 'docfilePath' to @"playerdata.plist", it gets set to NO. Nothing seems to change with the game in either case.</p>
  
  <p>SOLUTION:</p>
  
  <pre><code>-(void) newHighestLevel:(NSString*)path :(int)level{
        [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
        NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
        [self.data writeToFile:docfilePath atomically:YES];
  }
  </code></pre>
  
  <p>and in init</p>
  
  <pre><code>-(id) init{
  
       NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
       NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
       NSFileManager *fileManager = [NSFileManager defaultManager];
       if (![fileManager fileExistsAtPath:docfilePath]){
           NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"];
           [fileManager copyItemAtPath:sourcePath toPath:docfilePath error:nil];
       }
       self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
       return self;
  }
  </code></pre>