0
我最近一直致力於製作類似於股票iOS設備上的秒錶應用程序。我有計時器工作,但我希望它遵循像Apple的秒錶一樣的外觀和確切的時間。截至目前,我只是顯示秒數。據我所知,必須有某種將數字轉換爲00:00.00格式。如何使用我的秒錶顯示確切的時間?
import UIKit
class ViewController: UIViewController {
var counter = 0
var timer = Timer()
@IBOutlet weak var timerLbl: UILabel!
@IBAction func startBtn(sender: Any) {
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)
}
@IBAction func stopBtn(sender: Any) {
timer.invalidate()
counter = 0
}
func updateTimer() {
counter += 1
timerLbl.text = "(counter)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
可能重複[什麼格式的字符串,我在毫秒的iPhone日期字符串?](https://stackoverflow.com/questions/6456626/what-format-字符串 - 我使用毫秒 - 在日期字符串在iphone) – Wukerplank
@Wukerplank是Objective-C,我正在尋找一個答案在Swift中。 – Dexter
你一定在開玩笑吧。查找「NSDateFormatter」的文檔,並使用提供的答案中的格式字符串。 – Wukerplank