2017-06-29 15 views
1

グリッド構造体の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 


     } 
    } 
} 

に位置を設定しようとしてきた方法の全てをコメントしました私はそれにアクセスできませんか?

答えて

1

問題は、あなたの行のどれも実際にCell構造体のインスタンスにアクセスしていないことです。

ここにコードの機能があります。あなたが発生したエラーの詳細な説明のために、今すぐ

struct Cell { 
    var position: (Int,Int) 

    init(_ position: (Int,Int)) { 
     self.position = (0,0) 
    } 
} 

func positions(rows: Int, cols: Int) -> [(Int, Int)] { 
    return (0 ..< rows) 
     .map { zip([Int](repeating: $0, count: cols) , 0 ..< cols) } 
     .flatMap { $0 } 
     .map { ($0.0, $0.1) } 
} 

struct Grid { 
    var rows: Int = 10 
    var cols: Int = 10 
    var cells: [[Cell]] = [[Cell]]() 

    init(_ rows: Int, _ cols: Int) { 
     self.rows = rows 
     self.cols = cols 
     self.cells = Array.init(repeating: Array.init(repeating: Cell((0,0)), count: cols), count: cols) 

     positions(rows: rows, cols: cols).forEach { row, col in 
      cells[row][col].position = (row, col) 
     } 
    } 
} 

let g = Grid(1, 2) 
print(g.cells[0][1].position) 

:私は自分自身があなたのコードベースから取り残されているように見える余分なものを削除することができ

var position = cells(position: (row, col)) 

ここでは、どの細胞にも何も設定していない。代わりに、というグリッドを呼び出すようにしようとしています。グリッドは関数のように、パラメータposition: (Int, Int)と同じです。ここで

cells.position = (row, col) 

あなたのマトリックス上([[Cell]])のプロパティpositionを設定しようとしています。そして明らかに、スウィフトはそのような財産がその内蔵タイプArrayに存在しないと不平を言う。ここで

cells.position(row, col) 

あなたの行列にプロパティpositionを設定する([[Cell]])と二つのパラメータIntを持つ関数としてそれを呼び出すようにしようとしています。問題は上記と同様です。ここで

position *= cells.position(row, col) 

私はpositionがあなたのコード内で宣言されているようだがないので、何が起こっているのかわかりませんよ。私はそれがあなたのコードベースのどこか他の場所から来ていると思うし、それは単にタイプミスかもしれない。

+0

ありがとうございます。それは非常に便利です! – HashRocketSyntax

1

cells.positionにアクセスしようとしていますが、cellsは2次元配列です。

cells.position = (row, col) => value type of '[[Cell]] has no member position' 

セルをループして、それぞれの位置を設定できます。

だからあなたforEachループであなたの代わりに

cells[row][column].position = (row, col) 

書くことができ、それはそれを行う必要があります。

+0

ありがとうございます - それはコンパイルします。 [行]は配列の最初のレイヤーにアクセスし、[列]は第2レイヤーにアクセスしますか? – HashRocketSyntax

関連する問題