如何使用 xCode 中的字符串制作计时器

How Can I Make A Timer Using String In xCode

所以我正在尝试制作一个国际象棋计时器。我正在使用字符串在名为 storeTimed 的变量中生成 00:00。有没有办法让它使用计时器从那里开始倒计时?

这是我的代码:

我需要帮助的部分是 updateTimer() 函数。由于 storedTime 是一个试图作为 Int 传递的字符串,xCode 不喜欢它。老实说,我不太确定我能做什么。主要是在 else 语句处,但一般都是整个部分


class ChessTimer: UIViewController {

    @IBOutlet weak var playerTimer1: UILabel!
    @IBOutlet weak var playerTimer2: UILabel!
    
    var timer = Timer()
    
    var time = 10
    var isTimerRunning = false
    
    var storedTime = "00:00"
    override func viewDidLoad() {
        super.viewDidLoad()

       if isTimerRunning == false {
           runTimer()
       }
        
        
    }

    @IBAction func restartButton(_ sender: UIButton) {
        
        
        
    }
    
    @IBAction func pausePressed(_ sender: UIButton) {
        timer.invalidate()
    }
    
    @IBAction func settingsPressed(_ sender: UIButton) {
        
        performSegue(withIdentifier: "goToSettings", sender: self)
        
    }
    
    func runTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self,selector: (#selector(ChessTimer.updateTimer)),userInfo: nil, repeats: true)
           isTimerRunning = true
       }
    
    @objc func updateTimer() {
        if Int(storedTime) < 1 {
             timer.invalidate()
            playerTimer1.text = "00:00"
            playerTimer2.text = "00:00"
         }
            else {
        Int(storedTime)! -= 1
       playerTimer1.text = prodTimeString(time: TimeInterval(storedTime)!)
        
        }
        
     }
    
    func prodTimeString(time: TimeInterval) -> String {
        let prodMinutes = Int(time) / 60 % 60
        let prodSeconds = Int(time) % 60

        return String(format: "%02d:%02d", prodMinutes, prodSeconds)
    }
    
    
    @IBAction func playerButton1(_ sender: UIButton) {
    }
    
    
    @IBAction func playerButton2(_ sender: UIButton) {
    }
    
    
    @IBAction func unwindToVC1(sender: UIStoryboardSegue) {
        
        
        if let settingsController = sender.source as? SettingsController {
            playerTimer1.text = settingsController.storedTime
            playerTimer2.text = settingsController.storedTime
            
            storedTime = settingsController.storedTime
        
        }
    
   }

}

为简单起见,这里是代码。我已经删除了与讨论无关的功能-未实现的按钮等,

简单的想法是您使用数字(Int / Double / 其他)来存储您正在计数的变量,然后使用辅助函数以不同的格式呈现变量

import UIKit

class ChessTimer: UIViewController {
    var timer = Timer()

    var isTimerRunning = false

    var storedTime : Int = 0 // use an integer to store the time

    override func viewDidLoad() {
        super.viewDidLoad()

       if isTimerRunning == false {
           runTimer()
       }
    }

    func runTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self,selector: (#selector(ChessTimer.updateTimer)),userInfo: nil, repeats: true)
       isTimerRunning = true
   }

    @objc func updateTimer() {
        // because we're storing the value in an Int, we don't need to convert it here
        storedTime -= 1
        if storedTime < 1 {
            timer.invalidate()
            // use the helper function to format the result for zero time as well as everything else
            // that way, if you ever change it, you only have to change in one place
            playerTimer1.text = prodTimeString(0)
            playerTimer2.text = prodTimeString(0)
         }
         else {
            playerTimer1.text = prodTimeString(storedTime)
          }
     }

    func prodTimeString(_ time: Int) -> String {
        // because there's only one parameter, and it's obvious, add '_' then you don't need the label when you call it
        let prodMinutes = time / 60 % 60
        let prodSeconds = time % 60

        return String(format: "%02d:%02d", prodMinutes, prodSeconds)
    }
}