0

I'm working on a Pong game to lean some iPhone programming.

I have an NSString* playerName and int playerScore.

I get this when the game is over.

Now I need a way to store high scores on the user's device. However there are a couple caveats.

I only bother to store a score if it is a top 10 high score. And if the user's name is already in the list and the score is worth adding (eg > 10th score), the score is updated and the list s resorted by score.

I'm coming from C++ world.

In C++, I would use a simple file that I would parse, work with in memory, then rewrite the file.

What I would like to know though, is how this should be done in Obj-C. Is there a class or something that makes this sort of task simple and convenient?

Thanks

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • hm.. even i'd like to know if there is a class to make this task simple but when i did this, i used a plist file to which i'd read/write. – staticVoidMan Nov 09 '13 at 20:34

2 Answers2

1

Just use an NSArray of NSDictionary instances where the dictionaries contain your scores (as NSNumbers) and names. Then you can directly read and write this to disk (as a property list) using initWithContentsOfFile: and writeToFile:atomically:.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

Try this

create an NSMutableArray that will store user scores. Every time the game ends find the score in the NSMutableArray that has the 10 highest value. After that store both PlayerName and HighestScores using NSUserDefaults

NSUserDefaults will allow you to save objects in the user's device

NSString *PlayerName;
int PlayerScore;


//SAVING

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:PlayerName forKey:@"name"];

// saving an NSInteger
[prefs setInteger:PlayerScore forKey:@"score"];

//GETTING

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"name"];

// getting an NSInteger
int myInt = [prefs integerForKey:@"score"];

This is just some guide I think will help :)

Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42