execution was interrupted reason...

Does anyone know why I get this error when attempting to run my code? I get this error ->

execution was interrupted reason exc_bad_instruction (code=exc_i386_invop subcode=0x0)

each time I am trying to run it. I have only 1 day experience with Swift so please explain me with details why this occurs:



func levelCost(heights: [Int], maxJump: Int) -> Int{

var energyRequired = 0;

for (index,item) in heights.enumerate(){

if item != heights.indexOf(1){

let difference = heights.indexOf(index)! - heights.indexOf(index-1)!

if heights.indexOf(index) == heights.indexOf(index-1){

energyRequired = energyRequired + 1;

}

else if (heights.indexOf(index) > heights.indexOf(index-1)) && (abs(difference) <= maxJump){

energyRequired = energyRequired + abs(difference)*maxJump

}

else if (heights.indexOf(index) < heights.indexOf(index-1)) && (abs(difference) <= maxJump){

energyRequired = energyRequired + abs(difference)*maxJump

}

}

}

return energyRequired

}

levelCost([1, 1, 2, 2, 5, 2, 1, 1], maxJump: 3)

Does anyone know why I get this error when attempting to run my code?

This error is usually the result of the code doing something illegal that’s trapped by Swift. For example, consider this code:

let a = [1, 2, 3]
print(a[3])

which is never going to work because the index used on the second line is out of bounds.

  • When you build the Release configuration you get this trap.

  • When you build the Debug configuration you’ll get a nice error message.

fatal error: Index out of range

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
execution was interrupted reason...
 
 
Q