我正在尝试在Swift中创建一个计时器,我无法弄清楚该格式如何与NStimer一起使用。我想以00格式输出时间,所以当它增加时它就像:00 - > 01 - > 02 - > .....-> 10 - > 11 - > ... 这是我的代码:
import UIKit
import Foundation
@IBOutlet var label : UILabel!
@IBOutlet var label_c : UILabel!
@IBOutlet var button : UIButton!
var count : Double = 0.0
var time : NSTimer!
@IBAction func countI(){
time = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "countS", userInfo: nil, repeats: true)
}
@IBAction func countS(){
count += 0.01
let s = String(format: "%02d", count%100)
label.text = toString(count)
label_c.text = s
}
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.
}
答案 0 :(得分:0)
它看起来像是一个类型错误 - %d
需要一个整数但你传递的是双倍。
您可以将结果转换为Int
,这应该有效:
let s = String(format: "%02d", Int(count%100))