OK, excuse me if this is simple, but ... I am making a game for iPhone and I have a list of high scores -- well, not exactly a list, but a bunch of integers, all that I have saved using NSUserDefaults that work fine. My problem is that I have expanded my game, and with over 30 levels and 3 modes (90 high scores) I finally realized that even it works, this was getting ridiculous. I was searching online, but there is not much that tells you specifically about NSArrays, although they are mentioned prosperously throughout high score inquiries on Stack Overflow. I also realize that I probably would have to save each item as a NSUserDefault ... so would it help anyway? Unless there is some sort of NSUserDefaultArray... but hundreds of integers (and growing) just seems to be getting out of hand.
-
Could you add a code sample of how you're saving those integers? – willc2 May 22 '11 at 00:42
1 Answers
I'm hearing 2 questions here:
1) How to pack integers into an NSArray:
Check out Apple Docs "Collections Programming Topics, Arrays: Ordered Collections." It has handy code snippets.
Note that you'll have to wrap the integers into NSNumbers in order to store them in an array. See "Number and Value Programming Topics, Using Numbers" for code snippets.
2) How to persist (store) the high-score integers:
My guess is that most game programmers store this sort of thing in a database, which means you'll need to find out how to use Core Data/SQLite, which is fairly involved. But you'll want to know about this eventually anyway, and you'll be able to persist other game data this way, enhancing your app.
Incidentally, Core Data doesn't have an array data type. You have to use a "transformable" attr. If you make explicit h and m files for the managed object class, though, you can simply retype the property as an NSArray, like this:
@property (nonatomic, retain) NSArray *arrayHighScores;
Then you can just treat this attr as a regular NSArray.
Hillegasse's Cocoa Programming for Mac can give you backbone knowledge on Core Data, and there are other books out there that would help you apply it to the iPhone.
OR, check out this link: Storing custom objects in an NSMutableArray in NSUserDefaults