グリッド構造体のinit内で設定する必要があるCell構造体の値(position:、state :)がありますが、Cellの値を設定できないようです。他の構造体内で構造体の値を設定するには
struct Cell {
var position: (Int,Int)
var state: CellState
init(_ position: (Int,Int), _ state: CellState) {
self.position = (0,0)
self.state = .empty
}
}
func positions(rows: Int, cols: Int) -> [Position] {
return (0 ..< rows)
.map { zip([Int](repeating: $0, count: cols) , 0 ..< cols) }
.flatMap { $0 }
.map { Position(row: $0.0,col: $0.1) }
}
Iが明らかにセル構造体は、位置の特性を有し、なぜすることができ、私は(行、列)
struct Grid {
static let offsets: [Position] = [
(row: -1, col: 1), (row: 0, col: 1), (row: 1, col: 1),
(row: -1, col: 0), (row: 1, col: 0),
(row: -1, col: -1), (row: 0, col: -1), (row: 1, col: -1)
]
var rows: Int = 10
var cols: Int = 10
var cells: [[Cell]] = [[Cell]]()
init(_ rows: Int,
_ cols: Int,
cellInitializer: (Int, Int) -> CellState = { _,_ in .empty }) {
self.rows
self.cols
self.cells = [[Cell]](repeatElement([Cell](repeatElement(Cell((0,0), .empty), count: cols)),count: rows))
positions(rows: rows, cols: cols).forEach { row, col in
// var position = cells(position: (row, col)) => cannot call value of non-function type '[[Cell]]'
// cells.position = (row, col) => value type of '[[Cell]] has no member position'
// cells.position(row, col) => value type of '[[Cell]] has no member position'
// position *= cells.position(row, col) => closure cannot implicitly capture a mutating self parameter
}
}
}
に位置を設定しようとしてきた方法の全てをコメントしました私はそれにアクセスできませんか?
ありがとうございます。それは非常に便利です! – HashRocketSyntax