Maria 9905
Maria 9905

Reputation: 207

Array of Enums in Swift 3

I am trying to pass a 2D array of enums to my function as you can see below but I am getting the error:

error: cannot convert value of type '[Array]' to expected argument type '[[Piece]]

var won = hasWonTicTacToe(board: board)

How should this be done in Swift 3?

let board = [[0,0,0], [1,0,1], [1,1,0]]

enum Piece {
    case red
    case blue
    case empty
}

func hasWonTicTacToe(board: [[Piece]]) -> Piece {
    /*..more code here*/
}

Upvotes: 0

Views: 982

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

It's unclear what you mean [[0,0,0], [1,0,1], [1,1,0]] to mean, but if you mean 0 to be .red and 1 to be .blue, then what you meant was:

let board: [[Piece]] = [[.red,.red,.red], [.blue,.red,.blue], [.blue,.blue,.red]]

Enum values are themselves. They are not equivalent to integers.

Keep in mind that this is not "a 2D array." This is an array of arrays. That's a very different thing. There is no rule that says each row is the same length. If you want an actual matrix, you'd need to create that data structure; Swift doesn't have one built in.

Upvotes: 3

Related Questions