Sigabrt swift Xcode(制作计时器)

时间:2015-07-12 01:13:29

标签: xcode swift nstimer

我正在尝试制作一个秒表,我似乎无法理解为什么我的应用程序会有一个sigabrt。对不起,我不知道要包括什么,所以我把所有内容都包括在内。

var timer = NSTimer()
var minutes: Int = 0
var seconds: Int = 0
var fractions: Int = 0
var startStopWatch: Bool = true
var StopwatchString: String = ""
@IBOutlet weak var display: UILabel!

@IBAction func start(sender: UIButton) {

    if startStopWatch == true {
        NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updateStopwatch"), userInfo: nil, repeats: true)
        startStopWatch = false
    } else {
        timer.invalidate()
        startStopWatch = true
    }
}

@IBAction func reset(sender: UIButton) {

}

override func viewDidLoad() {
    super.viewDidLoad()
    display.text = "00:00.00"
}

func updateStopWatch() {
    fractions += 1
    if fractions == 100 {
        seconds += 1
        fractions = 0
    }

    if seconds == 60 {
        minutes += 1
        seconds = 0
    }


    let fractionsString = fractions > 9 ? "\(fractions)" : "0\(fractions)"
    let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
    let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"
    StopwatchString = "\(minutesString):\(secondsString).\(fractionsString)"
    display.text = StopwatchString
}

2 个答案:

答案 0 :(得分:0)

我将您的代码剪切并粘贴到项目中,当我点击开始按钮时,错误消息的第一行说:

  

- [My.ViewController updateStopwatch]:无法识别的选择器发送到实例0x7fe0034b3520

告诉你所有你需要知道的事情。您将updateStopwatch消息发送到视图控制器,但它没有实现该方法。我注意到你的ViewController确实实现了updateStopWatch方法。看起来像是一个基本的大写错误。

始终,始终,始终至少读取错误消息的第一行。

答案 1 :(得分:0)

以下是一些修正:

+我最初将timer变量更改为nil - 它将保存在start函数中。

+ start函数中的if语句如下所示:

如果startStopWatch == true
但它应该是这样的:

if(startStopWatch)

希望这有帮助:)

var timer = nil
var minutes = 0
var seconds = 0
var fractions = 0
var startStopWatch = true
var StopwatchString = ""
@IBOutlet weak var display: UILabel!

@IBAction func start(sender: UIButton) {
    if (startStopWatch == true) {
        timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updateStopwatch"), userInfo: nil, repeats: true)
        startStopWatch = false
//I added one thing. If the start button is pushed the text will change to stop. You can change this if you like.
        sender.setTitle("Stop", forState: .Normal)
//
    } else {
        timer.invalidate()
        startStopWatch = true
    }
}

@IBAction func reset(sender: UIButton) {
    display.text! = "00:00.00"
}

override func viewDidLoad() {
    super.viewDidLoad()
    display.text! = "00:00.00"
}

func updateStopWatch() {
    fractions++
    if (fractions == 100) {
        seconds++
        fractions = 0
    }

    if (seconds == 60) {
        minutes++
        seconds = 0
    }


    var fractionsString = fractions > 9 ? "\(fractions)" : "0\(fractions)"
    var secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
    var minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"
    StopwatchString = "\(minutesString):\(secondsString).\(fractionsString)"
    display.text! = StopwatchString
}