If I have a variable declared like var gameBoard: [Piece] = [], is there any way to add a subclass of Piece, called Queen, to the array?
I am using Piece to represent all pieces. Queen, Pawn, Bishop and such are all subclasses of Piece, and should be included on the board.
I remember doing this frequently in Objective C, where subclasses were able to be used in place of the superclass. But in my first attempts, it I am getting an error saying
'@lvalue $T11' is not identical to 'Piece`
Is this not possible anymore? Or would there need to be some use of generics that I cannot think of right now?
Edit
Here is the implementation of my board, including only the relevant parts.
struct GameBoard{
var board: [[Piece]]
init() {
board = []
for _ in 0...7{
var collumn: [Piece] = []
for _ in 0...7{
var piece = Piece(player: .None, board: self)
collumn.append(piece)
}
board.append(collumn)
}
}
subscript(coords:(Int, Int) ) -> Piece {
return board[coords.1][coords.0]
}
}
The code that fails is
var board = GameBoard()
var q = Queen(player: .Black, board: board)
board[(4,5)] = q //Throws the error
board.board[5][4] = q //Works
It seems to me that these two should work the same way. It may be an issue with the subscripting, but I am not completely sure.