使用NSUserDefaults在Swift中保存和加载高分 [英] Saving And Loading Up A Highscore In Swift Using NSUserDefaults

查看:101
本文介绍了使用NSUserDefaults在Swift中保存和加载高分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试比较我的得分和高分时遇到一些问题,看看哪一个更大,然后保存。我认为问题在于比较和保存高分和得分。非常感谢帮助!

I am having some problems while attempting to compare my score and highscore to see which one is bigger and then save it. I think the problem lies in the comparison and saving of the highscore and score. Help would be greatly appreciated!

//Comparison between score and highscore
if score > highscore {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    highscore = defaults.valueForKey("highscore")?.integerValue ?? 0
    defaults.setInteger(score, forKey: "highscore")
    defaults.synchronize()
    highscoreString = String(highscore)
    highscoreLabel.text = highscoreString  }

整页代码 - https://github.com/SRL311/Highscore/blob/master/Highscore
(很抱歉那里有令人困惑的大写字母和小写字母)

Full Page Code - https://github.com/SRL311/Highscore/blob/master/Highscore (Sorry about the confusing capitals and lowercases there)

推荐答案

您还可以使用getter和setter创建一个新的NSUserDefault属性,以自动检查您尝试保存它的分数是否高于实际高得分:

You can also create a new NSUserDefault property with a getter and a setter to automatically check if the score you are trying to save it is higher than the actual high score:

更新: Xcode 8.2.1•Swift 3.0.2

extension UserDefaults {
    static let highScoreIntegerKey = "highScoreInteger"
    static let highScoreDoubleKey = "highScoreDouble"
    var highScore: Int {
        get {
            print("High Score:", integer(forKey: UserDefaults.highScoreIntegerKey))
            return integer(forKey: UserDefaults.highScoreIntegerKey)
        }
        set {
            guard newValue > highScore
            else {
                print("\(newValue) ≤ \(highScore) Try again")
                return
            }
            set(newValue, forKey: UserDefaults.highScoreIntegerKey)
            print("New High Score:", highScore)
        }
    }
    func resetHighScore() {
        removeObject(forKey: UserDefaults.highScoreIntegerKey)
        print("removed object for highScoreIntegerKey")
    }
    var highScoreDouble: Double {
        get {
            return double(forKey: UserDefaults.highScoreDoubleKey)
        }
        set {
            guard newValue > highScoreDouble
            else {
                print("\(newValue) ≤ \(highScoreDouble) Try again")
                print("Try again")
                return
            }
            set(newValue, forKey: UserDefaults.highScoreDoubleKey)
            print("New High Score:", highScoreDouble)
        }
    }
    func resetHighScoreDouble() {
        removeObject(forKey: UserDefaults.highScoreDoubleKey)
        print("removed object for highScoreDoubleKey")
    }
}






在项目中测试(自Swift 2以来在游乐场不起作用):


testing in a Project (doesn't work in playground since Swift 2):

UserDefaults().resetHighScore()
UserDefaults().highScore       // High Score = 0
UserDefaults().highScore = 100 // New High Score = 100
UserDefaults().highScore = 50  // 50 < 100 Try again
UserDefaults().highScore       // 100
UserDefaults().highScore = 150 // New High Score = 150
UserDefaults().highScore       // 150 

Xcode 7.2•Swift 2.1.1

extension NSUserDefaults {
    var highScore: Int {
        get {
            print("High Score = " + integerForKey("highScore").description)
            return integerForKey("highScore")
        }
        set {
            guard newValue > highScore else { print("\(newValue) ≤ \(highScore) Try again")
                return
            }
            setInteger(newValue, forKey: "highScore")
            print("New High Score = \(highScore)")
        }
    }
    func resetHighScore() {
        removeObjectForKey("highScore")
        print("removed object for key highScore")
    }
    var highScoreDouble: Double {
        get {
             return doubleForKey("highScoreDouble")
        }
        set {
            guard newValue > highScoreDouble else { print("Try again")
                return
            }
            setDouble(newValue, forKey: "highScoreDouble")
            print("New High Score = \(highScoreDouble)")
        }
    }
    func resetHighScoreDouble() {
        removeObjectForKey("highScoreDouble")
        print("removed object for key highScoreDouble")
    }
}

这篇关于使用NSUserDefaults在Swift中保存和加载高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆