Swift EXC_BAD_INSTRUCTION错误

时间:2016-09-29 19:50:20

标签: swift optional

所以我收到错误#34;线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)"在以下'如果'线。

var doorsKnocked = 20
var sales = 0
var avgDoorsKnocked = 45.4
var avgSales = 1.2

    if Double(doorsKnocked/sales) < Double(avgDoorsKnocked/avgSales) {
        doorsPerSaleTodayLabel.textColor = UIColor(red: 43/255, green: 182/255, blue: 115/255, alpha: 1.0)
    }

我认为这与可选项有关,但我不知道修复。

1 个答案:

答案 0 :(得分:1)

所以它确实与我试图除以0的事实有关。为了解决这个问题,我将所有使用销售的行放在'if'子句中。可能不是最干净的方式,但希望这有助于其他人。

if sales != 0 {

    if Double(doorsKnocked/sales) < Double(avgDoorsKnocked/avgSales) {
    doorsPerSaleTodayLabel.textColor = UIColor(red: 43/255, green: 182/255, blue: 115/255, alpha: 1.0)
    }

    doorsPerSaleTodayLabel.text = "\(Int(round(1*Double((Double(doorsKnocked)/Double(sales))))/1))"
    distancePerSaleTodayLabel.text = "\(Double(round(10*(pausedDistanceInMiles/Double(sales)))/10))"
    timePerSaleTodayLabel.text = "\(Double(round(10*(timeWorkedInHoursDouble/Double(sales)))/10))"
    } else {

    }