Search code examples
iosswiftexc-bad-instruction

Swift 3 - 'EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)' Error


I am working on a maze game in swift 3 and it compiles but when I run it I get the EXC_BAD_INSTRUCTION error. I'm relatively new to swift so I don't know how to properly debug it. Here is the piece of code that is getting the error:

init(window:SKScene, width:Int, height:Int){
    widthOfBoard = width
    heightOfBoard = height


    for i in 0..<widthOfBoard {
        var temp:[cell]!
        for j in 0..<heightOfBoard {
            let tempCell = cell(x: i, y: j, boardWidth: widthOfBoard, boardHeight: heightOfBoard, screenSize: window.frame)
            temp.append(tempCell)
        }
        cells.append(temp)
    }
    stackOfCells.append(cells[0][0])
    recursiveMaze(currentX: 0, currentY: 0, window: window)
}

I am getting the error with cells.append(temp).

I am also getting the error in each of these locations.


Solution

  • temp is nil that causes the crash. Any carelessly written exclamation mark can cause a crash.

    You declared the variable temp but you have to initialize the array if you want to append items to it:

    var temp = [cell]()
    

    By the way: Class names are supposed to start with a capital letter.