0

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.

Henry Pigg
  • 90
  • 1
  • 1
  • 6

1 Answers1

3

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.

vadian
  • 274,689
  • 30
  • 353
  • 361