Reputation: 39
I am instantiating an array of UITextField's in the variable labels04. I loop to do some calculations. After the calculations, i attempt to use the index of the array to further calculate my algorithm but get a bad instruction error.
@IBOutlet var labels04: [UITextField]!
var gpacalc1:Int = 0
var gpacalcDivide:Int = 0
var convertIndexGPA:Int = 0
On the line
gpacalcDivide = gpacalculate / convertIndexGPA
i get a THREAD1:EXC_BAD_INSTRUCTION(CODE=EXC_I386_INVOP, subcode 0x0)
buttonContext
{
for label:UITextField in labels04 {
//calculations ...
gpacalculate = gpacalculate + gpacalc!
convertIndexGPA = labels04.index(of: label)!
gpacalcDivide = gpacalculate / convertIndexGPA
gpaTotalCalc.text! = "GPA: \(gpacalcDivide)"
}
}
I believe it has to do with using labels04.index(of: label)! command. Although it prints as an integer in the console, but when i attempt to use that value for an arithmetic operation I get prompted with the bad instruction error.
Is there a way out of this? Thank you
Upvotes: 0
Views: 815
Reputation: 37290
During the first iteration of your for loop, labels04.index(of: label)!
should equal 0 (i.e. the first index), so on the next line -- gpacalcDivide = gpacalculate / convertIndexGPA
-- you'd be dividing by 0 which would result in an error.
Upvotes: 3