gpt4 book ai didi

ios - 无法将类对象分配给 Swift 3 中另一个类对象的字典成员

转载 作者:行者123 更新时间:2023-11-28 15:57:10 24 4
gpt4 key购买 nike

我有一个 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

}

最佳答案

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

根据你上面的评论,我做了一个小测试来验证如果 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

关于ios - 无法将类对象分配给 Swift 3 中另一个类对象的字典成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com