简体   繁体   中英

Swift EXC_BAD_INSTRUCTION error

So I'm getting an error of "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)" on the following 'if' line.

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)
    }

I assume this has to do with an optional, but I don't know the fix.

So it did have to do with the fact that I was trying to divide by 0. To fix this, I put all of the lines that use sales in an 'if' clause. Probably not the cleanest way, but hopefully this helps someone else.

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 {

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM