这段代码的含义是:返回self.desk[number]中的alpha值。

huangapple go评论86阅读模式
英文:

meaning of this code: return self.desk[number]![alpha]

问题

self.desk[number]![alpha] 中的 ! 表示强制解包(force unwrapping)。这意味着它试图从可选值中取出一个非可选值,但如果该值为 nil,则会引发运行时错误。这种语法用于表示程序员相信该可选值一定包含非 nil 值,即使编译器认为它可能为空。在这个代码中,self.desk[number]![alpha] 试图访问 self.desk 字典中的元素,其中 number 是键,alpha 是嵌套字典中的键。如果 numberalpha 不存在于字典中,强制解包 ! 可能会导致运行时错误,因此需要小心使用。

英文:

please help me understand this part of the code. Why is ! located between two brackets []![] in this expression: self.desk[number]![alpha]?

class GameDesk {
    var desk : [Int: [String: Chessman]] = [:]

    init() {
        for i in 1...8 {
            desk[i] = [:]
        }
    }

    subscript(alpha: String, number: Int) -> Chessman? {
        get {
            return self.desk[number]![alpha]
        }
        set {
            if let chessman = newValue {
                self.setChessman(chess: chessman, coordinates: (alpha, number))
            } else {
                self.desk[number]![alpha] = nil
            }
        }
    }

    func setChessman(chess: Chessman, coordinates: (String, Int)){
        let rowRange = 1...8
        let colRange = "A"..."Z"
        if(rowRange.contains(coordinates.1) && colRange.contains(coordinates.0)) {
            self.desk[coordinates.1]![coordinates.0] = chess
            chess.setCoordinates(char: coordinates.0, num: coordinates.1)
        } else {
            print("Coordinates out of range")
        }
    }
}

答案1

得分: 1

desk 是一个具有 Int 键和另一个字典作为值的字典,该字典的键是 String,值是 Chessmansubscript 函数实现通过行号和列字符串设置或获取棋子。对字典进行查找会返回一个可选值(键可能不存在),但这段代码(有些危险地)假定它永远不会使用无效的 number 调用,并用 ! 进行强制解包。这使结果非可选,然后进行另一次字典查找以找到 alpha 处的棋子。这也可以是可选的,这就是为什么该方法返回一个可选值。这也意味着代码可以像 @vadian 在评论中说的那样使用 ?。这回答了您的问题,但值得一提的是,嵌套字典不是实现棋盘的好方法。二维数组会更简单和更高效。但这是一个完全不同的问题…

英文:

desk is a dictionary with a key of Int and a value which is another dictionary that itself has a key of String and a value of Chessman. The subscript function implements setting or getting a piece by the row number and column string. A lookup in a dictionary returns an optional value (the key might not exist) but this code is (somewhat dangerously) assuming that it will never be called with an invalid number and is force unwrapping the lookup with the !. That makes the result non-optional and another dictionary lookup is performed to find the chess piece at the alpha. That can also be optional which is why the method returns an optional. This also means the code could have used ? as @vadian said in a comment.

That answers your question, but it’s worth mentioning that nested dictionaries is not a very good way to implement a chess board. A two dimensional array would be simpler and more efficient. But that’s a whole separate other question…

huangapple
  • 本文由 发表于 2023年5月11日 01:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定