【发布时间】:2021-11-24 07:18:01
【问题描述】:
我正在用 Swift 做一个小游戏,但在使用类方法初始化属性时卡住了。
class Game {
var height: Int
var width: Int
var board: [[String]] = createBoard()
init(height: Int, width: Int) {
self.height = height
self.width = width
}
func createBoard() -> [[String]]{
var gameBoard: [[String]] = []
//working with width and height props from above
//filling the board and returning
return gameBoard
}
}
我如何通过函数 createBoard() 设置板值? (在创建板内我正在使用高度和宽度)
【问题讨论】:
-
width 和 height 在您的示例中是 var - 您可能想要更改它们以让 - 但如果您真的打算在运行时更改棋盘大小,请添加 didSet {} 处理程序并在那里使您的游戏板无效/重新创建.
标签: swift class properties class-method