我正在为我的应用程序开发一个倒数计时器,它从 15 开始倒计时。这是我的代码:
//
import UIKit
class ViewController2: UIViewController {
@IBOutlet weak var Timer: UILabel!
var countd = 15
//
//
override func viewDidLoad() {
super.viewDidLoad()
let time = NSTimer(timeInterval: 1.0, target: self, selector: "updateCounter", userInfo: nil, repeats: true)
Timer.text = String(countd)
NSRunLoop.mainRunLoop().addTimer(time, forMode: NSDefaultRunLoopMode)
}
func updateCounter(timer: NSTimer) {
Timer.text = String(countd)
if (countd > 0){
Timer.text = String(countd--)
Timer.text = String(countd)
}
}
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
但是,当我运行它时,一旦我到达带有计时器的部分,我就会看到标签闪烁 15 秒钟,但随后它立即因 SIGABRT 错误而崩溃。我能做什么?
更改
selector: "updateCounter"
自
selector: "updateCounter:"
请注意,在 Swift 2.2 中,很难犯这个错误(编译器会发出警告),而在 Swift 3 中,这将变得不可能(字符串文字选择器语法将变得非法)。如果您不了解字符串文字选择器语法,则可能需要立即更新到 Swift 2.2。