adman
adman

Reputation: 408

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.

Upvotes: 0

Views: 51

Answers (1)

adman
adman

Reputation: 408

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 {

    }

Upvotes: 1

Related Questions