Advice for app development iOS. ApplicationDidEnterBackground

前端 未结 2 1326
悲哀的现实
悲哀的现实 2021-01-29 06:08

I´m having some fun with swift and I´m trying to make a simple game. I got a few variables that changes during the game. What´s the best practice for saving those variables if <

2条回答
  •  长情又很酷
    2021-01-29 06:40

    If its just a few variables you want to store and manage you could use NSUserDefaults.

        // Create defaults
        let defaults = NSUserDefaults.standardUserDefaults()
    
        // Set an int for highScoreKey in our defaults
        defaults.setInteger(10, forKey: "highScoreKey")
    
        // Sync/Save the defaults we've changed
        defaults.synchronize()
    
        // Then to retrieve our highScoreKey default we've saved
        // We create an int to store the value that is in our default
        var highScoreInt = defaults.integerForKey("highScoreKey")
    
        println(highScoreInt)
        // Prints 10
    

    I'd set and save these values as soon as I have the values I need rather than in applicationDidEnterBackground though.

    NSUserDefaults Class Reference

提交回复
热议问题