ios - Changing from NSUserDefaults to NSCoding -


i'm trying create first sprite kit swift game, , have game data working using nsuserdefaults. want make more safe i'm attempting transition nscoding, nothing i've found online has helped me , has confused me (so sorry if answer exists, looked @ , wasn't able understand.)

this gamestate class:

class gamestate { var score: int var highscore: int var stars: int var sound: bool var cartype: int var xsize: double var ysize: double var gameover: bool  class var sharedinstance: gamestate {     struct singleton {         static let instance = gamestate()     }      return singleton.instance }  init() {     // init     score = 0     highscore = 0     stars = 0     cartype = 1     sound = true     xsize = 1     ysize = 1     gameover = false      // load game state     let defaults = nsuserdefaults.standarduserdefaults()      highscore = defaults.integerforkey("highscore")     stars = defaults.integerforkey("stars")     sound = defaults.boolforkey("sound")     cartype = defaults.integerforkey("cartype")     xsize = defaults.doubleforkey("xsize")     ysize = defaults.doubleforkey("ysize")     gameover = defaults.boolforkey("gameover") }   func savestate() {     // update highscore if current score greater     highscore = max(score, highscore)      // store in user defaults     let defaults = nsuserdefaults.standarduserdefaults()     defaults.setinteger(highscore, forkey: "highscore")     defaults.setinteger(stars, forkey: "stars")     defaults.setbool(sound, forkey: "sound")     defaults.setinteger(cartype, forkey: "cartype")     defaults.setdouble(xsize, forkey: "xsize")     defaults.setdouble(ysize, forkey: "ysize")     defaults.setbool(gameover, forkey: "gameover")     nsuserdefaults.standarduserdefaults().synchronize() } } 

and save , load data in other classes using:

gamestate.sharedinstance.***** 

how go changing implement nscoding , load , save data other classes? thanks.

the class want save has conform nscoding protocol , implement required init(coder adecoder: nscoder) , func encodewithcoder(acoder: nscoder).

here's simplified example:

class gamestate: nsobject, nscoding {      var score: int?     var gameover: bool?      init(score: int, gameover: bool) {         super.init()         self.score = score         self.gameover = gameover     }      required init(coder adecoder: nscoder) {         self.score = adecoder.decodeobjectforkey("score") as? int         self.gameover = adecoder.decodeobjectforkey("gameover") as? bool     }      func encodewithcoder(acoder: nscoder) {         acoder.encodeobject(self.score, forkey: "score")         acoder.encodeobject(self.gameover, forkey: "gameover")     }  }  let mygamestate = gamestate(score: 42, gameover: true) 

then can use nskeyedarchiver , nskeyedunarchiver, example in appdelegate, save , load object data:

func applicationwillterminate(anotification: nsnotification) {     nskeyedarchiver.archiverootobject(mygamestate, tofile: "a/file/path") }  func applicationdidfinishlaunching(anotification: nsnotification) {     if let data = nskeyedunarchiver.unarchiveobjectwithfile("a/file/path") as? gamestate {         mygamestate = data     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -