0

I have a plist file called playerData that includes a number object at index 0 indicating the highest level completed. After loading the view I read this object's integer value that is used throughout the game logic. If the player wins the game I would like to increment this number and write it to the plist file. here is the code I have (contained in an if statement)

levelNumber++;
NSNumber *levelNSNum = [[NSNumber alloc] initWithInteger:levelNumber];
[playerData replaceObjectAtIndex:0 withObject:levelNSNum];
[playerData writeToFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PlayerData.plist"] atomically:YES];
NSLog(@"%i should be written to file", levelNumber);

the log works so I know the conditions of the if statement have been met and the value is not the same as the one that was previously in the plist file, but for some reason this data is not being written over that data.

I am relatively new to this so I could be making an easy, stupid mistake I just can't seem to track down an answer. Thank you for your help!

Nairb555
  • 3
  • 2

1 Answers1

1

You're trying to write to your bundle, which is read-only after the app is installed. You should write to somewhere in your app sandbox instead, such as in your Library/Application Support directory. (You can use - [NSFileManager URLsForDirectory:inDomains:] with NSApplicationSupportDirectory to find this path; be sure to create the directory before you try to write to it.)

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • Thank you very much, I figured it was something simple like that but as I said, this is something really new to me. Thanks again for your quick and helpful response. – Nairb555 Jun 30 '12 at 15:04