Unable to assign class objects to dictionary member of another class object in Swift 3

105 Views Asked by At

I have a class ChessSquare with its own attributes and methods. I have another class ChessBoard, which has an attribute of dictionary type.

class ChessBoard {
    var squares: [ Int : ChessSquare ] = [:]
    .....

One of the methods of the ChessBoard class builds the chessSquare object and assigns it to chessBoard object as below.

self.squares[squareName] = chessSquare

Before Swift3 this code was working fine. After upgrading to Swift3 the assignment stopped working.

Using breakpoints I see that chessSquare objects are built as expected. squareName variable has the expected value. The "self", which is the chessBoard object has proper initial values. But the dictionary assignment gets skipped without any error in the above mentioned code.

Is this anything related to Swift3. I browsed and could not get specific solution to my case.


Adding more code for clarity.

class ChessSquare {
    let squareColor: UIColor
    let squareShade: Int
    let squareName: Int
    let squareSize: CGSize
    let minXY: CGPoint
    let maxXY: CGPoint
    let squareOrigin: CGPoint
    var hasPiece = false
    weak var chessPiece: ChessPiece?
    let squareSprite: SKSpriteNode
    let squareType: SquareType
    //more data members

    init(squareColor: UIColor, squareShade: Int, squareName: Int, squareSize: CGSize, minXY: CGPoint, maxXY: CGPoint, squareOrigin: CGPoint, squareSprite: SKSpriteNode, squareType: SquareType) {
        self.squareColor = squareColor
        self.squareShade = squareShade
        self.squareName = squareName
        self.squareSize = squareSize
        self.minXY = minXY
        self.maxXY = maxXY
        self.squareOrigin = squareOrigin
        self.squareSprite = squareSprite
        self.squareType = squareType
    }

    //more methods

}

class ChessBoard {
    var squares: [ Int : ChessSquare ] = [:]
    var byteBoard: [UInt8] = [UInt8](repeating: UInt8(0), count: 66)
    var byteBoardHashVal: Int = 0
    // [ byteBoardHashVal : ((byteBoard, evalnVal), repCnt) ]
    var byteBoards: [Int : (([UInt8],Double?), Int)] = [:]
    var TT: [Int : Double] = [:]
    var TTReuseCount = 0
    var whitePawnlessFileByte: UInt8 = 255
    var blackPawnlessFileByte: UInt8 = 255

    var maxXY: CGPoint = CGPoint()
    var minXY: CGPoint = CGPoint()

    var oldTouchedSquare: ChessSquare?

    init() {
        squares = [:]
        byteBoard = [UInt8](repeating: UInt8(0), count: 66)
        byteBoardHashVal = 0
        byteBoards = [:]
        TT = [:]
        TTReuseCount = 0
        whitePawnlessFileByte = 255
        blackPawnlessFileByte = 255

    }

    func drawFlippedChessBoard(_ view: SKView, scene: GameScene) {
        ....
        ....
        for row in 0..<8 {
            for col in 0..<8 {
                let squareName = row * 8 + col

                ....

                let chessSquare = ChessSquare(squareColor: currentColor, squareShade: squareShade, squareName: squareName, squareSize: squareSize, minXY: minXY, maxXY: maxXY, squareOrigin: squareOrigin, squareSprite: squareSprite, squareType: squareType)

                ....

                self.squares[squareName] = chessSquare

                ....
            }
            ....
        }
        ....
    }

    //more methods

}
1

There are 1 best solutions below

0
On

I have a difficulty in understanding if squareName is a string or a integer. It sounds like a string, I would suggest to name it chessSquareIndex or squareIndex if in case it is Int and you want to index chess square.

Based on your comment above I did a small test to verify that it works correctly if squareName is Int and here is the result,

class ChessSquare { }

class ChessBoard {
    var squares: [ Int : ChessSquare ] = [:]

    func getChessSquare(at squareIndex: Int) {
        let chessSquare = ChessSquare()
       return squares[squareIndex] = chessSquare
    }
}

And this seems to work fine with Swift 3. Also notice that it is not required to call self, unless you are inside escaping closures.