我正在为我的应用程序开始倒数计时器,从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错误。我该怎么办?
答案 0 :(得分:0)
更改
selector: "updateCounter"
到
selector: "updateCounter:"
请注意,在Swift 2.2中,很难犯这个错误(编译器会发出警告),而在Swift 3中它将变得不可能(字符串文字选择器语法将变为非法)。如果您不了解字符串文字选择器语法,您可能希望立即更新到Swift 2.2 。