我希望标签foxmill使用用户默认值存储高分。显然如果有一个新的高分我希望它被替换。问题是totalTime(跟踪得分的是)是一个double而不是int。所有教程都使用int作为高分教程。
import UIKit
class winViewController: UIViewController {
@IBOutlet var score2: UILabel!
@IBOutlet var winningLabel: UILabel!
@IBOutlet var foxMill: UILabel!
@IBOutlet var hhighscore: UILabel!
public var LebelText: String?
public var LebelText2: String?
public var LebelText3: String?
public var LebelText4: String?
override func viewDidLoad() {
super.viewDidLoad()
timeCalculation()
loadState()
}
func saveScore(score: Double) {
// Instantiate user defaults
let userDefaults:UserDefaults = UserDefaults.standard
// Set your score
userDefaults.set(score, forKey: "highScore")
// Sync user defaults
userDefaults.synchronize()
}
func loadState() {
let userDefaults = UserDefaults.standard
let score = userDefaults.double(forKey: "highScore")
foxMill.text = "High Score: \(score)"
}
func timeCalculation(){
guard let unwrapedText = self.LebelText2 else {
return
}
guard let unwrapedText2 = self.LebelText else {
return
}
guard let unwrapedText3 = self.LebelText3 else {
return
}
guard let unwrapedText4 = self.LebelText4 else {
return
}
if let myInt = Double(unwrapedText), let myInt2 = Double(unwrapedText2), let myInt3 = Double(unwrapedText3), let myInt4 = Double(unwrapedText4)
{
var totalTime = myInt + myInt2 + myInt3 + myInt4
self.winningLabel.text = "You won"+"\n"+"Reaction time :" + String(totalTime) + " Seconds"
saveScore(score: totalTime)
}}}
答案 0 :(得分:0)
您只需使用正确的密钥保存该值,然后在检索时,确保以Double
:
func saveScore(score: Double) {
// Instantiate user defaults
let userDefaults:UserDefaults = UserDefaults.standard
// Set your score
userDefaults.set(score, forKey: "highScore")
// Sync user defaults
userDefaults.synchronize()
}
现在,当您更新分数值时,当您说...(unwrappedText
)时,请确保保存用户默认值
if let myInt = Double(unwrapedText), let myInt2 = Double(unwrapedText2), let myInt3 = Double(unwrapedText3), let myInt4 = Double(unwrapedText4)
// HERE you do whatever it was you originally did with the text, but also save a value
saveScore(score: myInt)
{
现在下次加载应用时,加载分数:
func loadScore() {
// Instantiate User Defaults
let userDefaults = UserDefaults.standard
let score = userDefaults.double(forKey: "highScore")
}
当你想要一个Double
而不是Integer
如果您希望foxMill
标签在加载时显示此高分,则您需要做两件事。首先确保在应用关闭之前,您可以通过同步UserDefaults
来保存状态,在applicationDidEnterBackground
中查看applicationWillTerminate
和AppDelegate
等方法 - 但这有点超出了你的问题的范围。现在,我假设你上次通过调用上面列出的saveScore(score:)
函数退出应用程序时已经正确保存了状态。因此,现在您要检索分数并在加载时更新foxMill
标签。你会做这样的事情:
您可以通过修改上述loadScore
方法来执行此类操作:
func loadScore() {
// Instantiate User Defaults
let userDefaults = UserDefaults.standard
let score = userDefaults.double(forKey: "highScore")
foxMill.text = "High Score: \(score)"
}
然后在viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
loadScore()
}
或者您可以更改方法以返回一个字符串,然后将其分配给标签的文本:
func loadScore() -> String {
// Instantiate User Defaults
let userDefaults = UserDefaults.standard
let score = userDefaults.double(forKey: "highScore")
return "High Score: \(score)"
}
然后在viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
foxMill.text = loadScore()
}