user3897399 user3897399 - 1 year ago 331
Swift Question

sprite Kit save highest score

I build a game using Sprite Kit(with swift xCode 6 beta 4) and I need to keep the highest score that the player reached(so just an one Int). I know there is a way to do it using NSCoding, I googled it and found only Objective-C tutorials/guides which I can't use because I don't know objective-c at all, and maybe there are better ways that came in late versions of xCode. Any help will be appreciated!

Answer Source

You can save the highest score in NSUserDefaults. It is used to persist small amount of data and really easy to use.You can save the highest score like

// To save highest score
var highestScore:Int = 20
NSUserDefaults.standardUserDefaults().setObject(highestScore, forKey:"HighestScore")

// To get the saved score
var savedScore: Int = NSUserDefaults.standardUserDefaults().objectForKey("HighestScore") as Int

NSUserDefaults is mainly used for persisting these kind of data like high-score of user.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download