2

I'm writing an iPhone game and i want to save/load user settings (such as music loudness for example) and also i want to store the game progress.

For each level i want to have some struct object representing it state - is it opened, is it passed, it's time and so on.

I'm using cocos2d.

I found i can use NSUserDefaults class for storing game settings but i'm not sure it's good to use it for storing game progress.

Any help is welcome!

Andrew
  • 24,218
  • 13
  • 61
  • 90

2 Answers2

3

Use NSUserDefaults. But use them to serialize whole objects and load them back.

Override encodeWithCoder: and initWithCoder: for the classes of objects whose states you want to store. By this way you can directly store/serialize a whole object to the disk and construct them back again.

Also this way, you need not touch existing methods, you just have to add extra methods to each class that needs to be stored.

  • Sorry for the late response - was doing some other things. Can you provide some example ? Let say i have an NSObject inherited class which state i want to save – Andrew Jan 27 '11 at 16:40
1

NSUserDefaults is good for storing simple settings, but for full game state you might want to look into using something like Core Data.

Jeff Ames
  • 2,044
  • 13
  • 18