Robert
Robert

Reputation: 38213

iPhone: How can I store a high score to disk

I have a int which is my high score:

NSInteger highScore;

I want to store it persistently to disk

Do I have to convert it to a string, then write the string to a file, then parse it back when I want to read it. Or is there a better way to store small amounts of data?

Upvotes: 0

Views: 274

Answers (2)

Roe
Roe

Reputation: 44

You can write the NSInteger to a file withougt converting it to a string like so:

NSInteger foo = 134;
fwrite(&foo, sizeof(NSInteger),1, filePointer);

Then you can just read it back again using fread the same way.

Upvotes: 0

Douglas
Douglas

Reputation: 37761

Try saving the integer in NSUserDefaults.

There is an example of someone storing an integer in there here.

Upvotes: 3

Related Questions