【问题标题】:Swift 3: How do you save a high score programmatically?Swift 3:如何以编程方式保存高分?
【发布时间】:2016-11-02 07:59:27
【问题描述】:

我有一个增加分数的按钮,如果它是最高分数,那么即使在应用程序关闭并再次打开后,该数字也会保存为最高分数。

这部分工作正常,但是在我重新打开应用程序并单击按钮后,我的高分被重置并等于分数。我认为这是因为当我应该调用我的 HighscoreDefault 时我调用了我的 highscore 变量,但我不知道你如何访问该值......如果这甚至是问题。

重新打开应用后,如何防止高分变为低于自身的值?

import UIKit

class ViewController: UIViewController {

    let phrase = "Your Score: "
    var increasingNum = 0
    let otherphrase = "High Score: "
    var highscore = 0
    var label: UILabel?
    var otherlabel: UILabel?
    let HighscoreDefault = UserDefaults.standard

    override func viewDidLoad() {

        super.viewDidLoad()

        let label = UILabel(frame: CGRect(x: self.view.frame.size.width / 2 - 100, y: 100, width: 200, height: 20))
        label.textAlignment = .center
        label.text = "\(phrase) \(increasingNum)"
        self.view.addSubview(label)
        self.label = label

        let otherlabel = UILabel(frame: CGRect(x: self.view.frame.size.width / 2 - 100, y: 200, width: 200, height: 20))
        otherlabel.textAlignment = .center
        otherlabel.text = "\(otherphrase) \(highscore)"
        self.view.addSubview(otherlabel)
        self.otherlabel = otherlabel

        let button = UIButton(frame: CGRect(x: self.view.frame.size.width / 2 - 150, y: self.view.frame.size.height / 2 - 50, width: 300, height: 100))
        button.backgroundColor = UIColor.cyan
        button.addTarget(self, action: #selector(scoreGoUp), for: .touchUpInside)
        self.view.addSubview(button)

        if let highscore = HighscoreDefault.value(forKey: "highscore") {

            otherlabel.text = "\(otherphrase) \(highscore)"
        }
    }

    func scoreGoUp (sender: UIButton){

        increasingNum += 1
        self.label?.text = "\(phrase) \(increasingNum)"
        if (increasingNum > highscore){

            highscore = increasingNum
            self.otherlabel?.text = "\(otherphrase) \(highscore)"
            HighscoreDefault.set(highscore, forKey: "highscore")
        }
    }
}

【问题讨论】:

  • 首先不要在UserDefaults 中使用valueForKey,除非你真的知道自己在做什么。有专门的方法integer(forKey:

标签: ios iphone swift swift3


【解决方案1】:

在我看来,保存highscore 最方便的方法是实现这样的计算变量:

var highScore: Int {
    get {
        return UserDefaults.standard.integer(forKey: "highScore")
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "highScore")
    }
}

这样,在控制器或单例中拥有该变量后,您就不会再考虑 UserDefaults 了。

【讨论】:

    【解决方案2】:

    代替

    if let highscore = HighscoreDefault.value(forKey: "highscore") {
    
                otherlabel.text = "\(otherphrase) \(highscore)"
            }
    

    在 ViewDidLoad 的代码中添加这一行

    if let highscore = HighscoreDefault.value(forKey: "highscore") {
    
            otherlabel.text = "\(otherphrase) \(highscore)"
            self.highscore = highscore as! Int
        }
    

    【讨论】:

      【解决方案3】:

      当您重新打开您的应用程序时,UserDefault's 可能会保存您的 highScore 但 您的代码没有将其分配给您的 ViewController's highscore 变量

      if let highscore = HighscoreDefault.value(forKey: "highscore") as? Int {
              self.highscore = highscore
              otherlabel.text = "\(otherphrase) \(highscore)"
      }
      

      【讨论】:

        【解决方案4】:

        highscore 默认值为 0,因此每次第一次调用 if (increasingNum > highscore) 的操作应始终为 true。您需要在代码中的某处执行什么操作才能将您的 HighscoreDefault.value(forKey: "highscore") as? Int 分配给 highscore

        override func viewDidLoad() {
        
            super.viewDidLoad()
        
            let label = UILabel(frame: CGRect(x: self.view.frame.size.width / 2 - 100, y: 100, width: 200, height: 20))
            label.textAlignment = .center
            label.text = "\(phrase) \(increasingNum)"
            self.view.addSubview(label)
            self.label = label
        
            let otherlabel = UILabel(frame: CGRect(x: self.view.frame.size.width / 2 - 100, y: 200, width: 200, height: 20))
            otherlabel.textAlignment = .center
            otherlabel.text = "\(otherphrase) \(highscore)"
            self.view.addSubview(otherlabel)
            self.otherlabel = otherlabel
        
            let button = UIButton(frame: CGRect(x: self.view.frame.size.width / 2 - 150, y: self.view.frame.size.height / 2 - 50, width: 300, height: 100))
            button.backgroundColor = UIColor.cyan
            button.addTarget(self, action: #selector(scoreGoUp), for: .touchUpInside)
            self.view.addSubview(button)
        
            if let highscore = HighscoreDefault.value(forKey: "highscore") as? Int {
                // here is the extra job!!
                self.highscore = highscore
                otherlabel.text = "\(otherphrase) \(highscore)"
            }
        }
        

        希望对您有所帮助。

        【讨论】:

          【解决方案5】:

          在这里初始化你的高分:

           if let highscoreLast = HighscoreDefault.value(forKey: "highscore") {
          
              otherlabel.text = "\(otherphrase) \(highscore)"
              // you should get your highscore here               
              highscore = highscoreLast
          
              }
          

          【讨论】:

            【解决方案6】:

            highscore 必须是 int 而非 int 的数组
            你应该对这个数组进行排序(升序或降序) 如果新分数 > 数组中的最小项目和 array.count > 你的高核心默认计数 - > 你用新分数更改这个项目。并保存它们
            if array.count

            【讨论】:

            • 我认为你只是想多了(BTW 没有投反对票)
            • 无需创建数组只保存1个值(最高分)
            猜你喜欢
            • 2018-02-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-04
            • 2010-12-16
            • 1970-01-01
            • 2017-05-23
            • 1970-01-01
            相关资源
            最近更新 更多