Search code examples
swiftoption-typeunwrap

Swift: "Value of optional type '(Int, Int)?' not unwrapped; did you mean to use '!' or '?'?


class Game {
    var board: [[Int]]!
    var turns: Int!
    let coordinates = [1:(0,0), 2:(0,1), 3:(0,2), 4:(1,0), 5:(1,1), 6:(1,2), 7:(2,0), 8:(2,1), 9:(2,2)]

    init() {
        self.reset()
    }

    func reset() {
        self.board = [[0,0,0],[0,0,0],[0,0,0]]
        self.turns = 0
    }

    func moveByPlayer(player: Int, space: Int) {
        let spaceDictionary = [1:(0,0), 2:(0,1), 3:(0,2), 4:(1,0), 5:(1,1), 6:(1,2), 7:(2,0), 8:(2,1), 9:(2,2)]
        if let spaceTuple = spaceDictionary[space] {
            if self.board[spaceTuple.0][spaceTuple.1] == 0 {
                self.board[spaceTuple.0][spaceTuple.1] = player
            }
        }
        print(self.board)
        self.turns = self.turns + 1
    }

    func checkForWin(space: Int) {
        let lines = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]]
        for index in 0..<lines.count {
            let x = self.coordinates[lines[index][0]]
            let y = self.coordinates[lines[index][1]]
            let z = self.coordinates[lines[index][2]]
            **if(self.board[x.0][x.1] != 0 && self.board[x.0][x.1] == self.board[y.0][y.1] && self.board[x.0][x.1] == self.board[z.0][z.1]){
                return self.board[x.0][x.1]**
            }
        }
    }
}

I am getting an error stating:

"Value of optional type (Int, Int)? not unwrapped"

where the code is supposed to be bolded but didn't format. I have a very similar implementation of an "unwrapped" dictionary in the previous function implementation but do not get the error. I am new to learning how Swift works. Can someone educate more on type casting and unwrapping of optionals in this context?


Solution

  • The problem is being caused by these lines:

    let x = self.coordinates[lines[index][0]]
    let y = self.coordinates[lines[index][1]]
    let z = self.coordinates[lines[index][2]]
    

    x, y, and z are all optional because a dictionary always returns an optional when looking up a value by key because the key might not exist even if you know they do.

    One solution is to do:

    if let x = self.coordinates[lines[index][0]], let y = self.coordinates[lines[index][1]], let z = self.coordinates[lines[index][2]] {
        if self.board[x.0][x.1] != 0 && self.board[x.0][x.1] == self.board[y.0][y.1] && self.board[x.0][x.1] == self.board[z.0][z.1] {
            return self.board[x.0][x.1]
        }
    }
    

    This ensures that x, y, and z are properly unwrapped.

    Another option would be to use the ?? operator. Change:

    let x = self.coordinates[lines[index][0]]
    

    to something line:

    let x = self.coordinates[lines[index][0]] ?? (0, 0)
    

    Do this for y and z as well. Then you won't get the error.