ios Swift - 使用 NSUserDefaults 保存高分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25269686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:48:54  来源:igfitidea点击:

Swift - Saving highscore using NSUserDefaults

iosxcodeuiviewcontrollerswiftnsuserdefaults

提问by user2397282

I'm using Swift to make a game. I want to save the users high score using NSUserDefaults. I know how to create a new NSUserDefaults variable in my AppDelegate file:

我正在使用 Swift 制作游戏。我想使用 NSUserDefaults 保存用户的高分。我知道如何在我的 AppDelegate 文件中创建一个新的 NSUserDefaults 变量:

let highscore: NSUserDefaults = NSUserDefaults.standardUserDefaults()

But how do I set/get this in my view controllers?

但是如何在我的视图控制器中设置/获取它?

回答by beeef

At first, NSUserDefaults is a dictionary (NSDictionary I think). Every app has its own user defaults, so you cannot access the user defaults from any other app.

起初, NSUserDefaults 是一个字典(我认为是 NSDictionary)。每个应用程序都有自己的用户默认值,因此您无法从任何其他应用程序访问用户默认值。

If the user (the one who plays your game) makes a new highscore, you have to save that highscore like this:

如果用户(玩你游戏的人)创造了一个新的高分,你必须像这样保存这个高分:

let highscore = 1000
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(highscore, forKey: "highscore")
userDefaults.synchronize() // don't forget this!!!!

Then, when you want to get the best highscore the user made, you have to "read" the highscore from the dictionary like this:

然后,当您想获得用户制作的最佳高分时,您必须像这样从字典中“读取”高分:

if let highscore = userDefaults.valueForKey("highscore") {
    // do something here when a highscore exists
}
else {
    // no highscore exists
}

回答by Ashok R

In Swift 3.0

在 Swift 3.0 中

let highScore = 1000
let userDefults = UserDefaults.standard //returns shared defaults object.

Saving:

保存:

userDefults.set(highScore, forKey: "highScore") //Sets the value of the specified default key to the specified integer value.

retrieving:

检索:

if let highScore = userDefults.value(forKey: "highScore") { //Returns the integer value associated with the specified key.
        //do something here when a highscore exists
    } else {
        //no highscore exists
}

回答by Mithra Singam

Swift 4

斯威夫特 4

Set Value

设定值

UserDefaults.standard.set("TEST", forKey: "Key")//setString

UserDefaults.standard.set("TEST", forKey: "Key")//设置字符串

Retrieve

取回

UserDefaults.standard.string(forKey: "Key")//getString

UserDefaults.standard.string(forKey: "Key")//获取字符串

Remove

消除

UserDefaults.standard.removeObject(forKey: "Key")

UserDefaults.standard.removeObject(forKey: "Key")

回答by jigar parmar

var defaults=NSUserDefaults()
var highscore=defaults.integerForKey("highscore")

if(Score>highscore)
{
    defaults.setInteger(Score, forKey: "highscore")
}
var highscoreshow=defaults.integerForKey("highscore")

lblHighScore.text="\(highscoreshow)
println("hhScore reported: \(lblHighScore.text)")
lblPlayScore.text="\(Score)"
println("play reported: \(lblPlayScore.text)")

回答by Alvaro Fernandez Arjona

You can use below piece of code:

您可以使用以下代码:

// Your score var

var score:Int = 0

//save

score = NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "score")

// load without starting value nil problems

if NSUserDefaults.standardUserDefaults().setIntergerForKey:("score") != nil {

score = NSUserDefaults.standardUserDefaults().setIntergerForKey:("score")

}