2017-06-29 62 views
1

我有需要在我的網格結構的init中設置的Cell結構值(position :, state :),但我似乎無法設置Cell的這些值。Swift在另一個結構中設置一個結構值初始化

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) } 
} 

我評論了這一切,我試圖設置的位置(行,列)

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) 

在這裏,我們試圖在你的矩陣([[Cell]])設置屬性position,並把它作爲兩個參數Int的功能。問題與上面類似。

position *= cells.position(row, col) 

在這裏,我不能告訴是怎麼回事,因爲position不似乎已經在你的代碼被宣佈。我想它來自你的代碼庫中的其他地方,或者它可能只是一個錯字。

+0

謝謝你的解釋。這非常有幫助! – HashRocketSyntax

1

這裏的問題是您正在嘗試訪問cells.position,但cells是一個二維數組。

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

您可以遍歷單元格並設置每個單元格的位置。

因此,在您forEach循環,你可以寫,而不是

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

而且應該這樣做。

+0

謝謝 - 編譯。那麼[row]訪問數組的第一層,[column]訪問第二層? – HashRocketSyntax