无法将类对象分配给Swift 3

时间:2017-01-09 01:01:28

标签: ios swift swift3 chess

我有一个ChessSquare类,它有自己的属性和方法。我有另一个类ChessBoard,它具有字典类型的属性。

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

ChessBoard类的一个方法构建chessSquare对象并将其分配给chessBoard对象,如下所示。

self.squares[squareName] = chessSquare

在Swift3之前,这段代码工作正常。 升级到Swift3后,作业停止了。

使用断点我看到chessSquare对象是按预期构建的。 squareName变量具有期望值。 " self",这是chessBoard对象具有正确的初始值。但是在上面提到的代码中跳过了字典赋值而没有任何错误。

这与Swift3有什么关系吗?我浏览了,无法得到具体解决方案。

为了清晰起见,添加更多代码。

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 个答案:

答案 0 :(得分:1)

我很难理解squareName是字符串还是整数。它听起来像一个字符串,我建议将其命名为chessSquareIndex或squareIndex,如果它是Int并且你想索引国际象棋方块。

根据你上面的评论我做了一个小测试来验证它是否正常如果squareName是Int并且这是结果,

class ChessSquare { }

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

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

这似乎与Swift 3一起正常工作。另请注意,除非您在转义闭包内,否则不需要调用self