1

I have Cell struct values (position:, state:) which need to be set within the init of my Grid struct, but I can't seem to set these values of 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've commented all of the ways that I've tried to set the position to (row, col)

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


        }
    }
}

Clearly the Cell struct has a property of position, so why can't I access it?

2 Answers 2

1

The problem is that none of your lines are actually accessing instances of your Cell struct.

Here's a functioning adaptation of your code. I allowed myself to remove extra stuff that seem to have been left out from your codebase:

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)

Now, for a more detailed explanation of the errors you encountered:

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

Here you're not setting anything on any cell. Instead, you're trying to call your grid as if it was a function, with a parameter position: (Int, Int).

cells.position = (row, col)

Here you're trying to set a property position on your matrix ([[Cell]]). And obviously, Swift complains that such property does not exists in its builtin type Array.

cells.position(row, col)

Here you're trying to set a property position on your matrix ([[Cell]]) and call it as a function with two parameters Int. The problem is similar as above.

position *= cells.position(row, col)

Here I'm can't tell what's going on, since position does not seems to have been declared in your code. I guess it comes from elsewhere in your codebase, or maybe it's merely a typo.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanations. That's very helpful!
1

The issue here is that you are trying to access cells.position but cells is a two-dimensional array.

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

You could loop through the cells and set the position of each one.

So in your forEach loop you could write instead

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

and that should do it.

1 Comment

Thank you - that compiles. So does [row] access the first layer of the array, and [column] accesses the 2nd layer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.