Hi all - I have a button that when pressed should use the …subscript… notation to call the setter function - but I don’t know how to code it below is the code for the struct of the Array of a custom struct called Letters:
In playground it works which makes me think it’s a scoping issue having the call in the Button portion of the code…
…
struct LetterGrid {
let rows = 6
let columns = 5
var grid: [Letter]
init(rows: Int, columns: Int) {
grid = Array(repeating: Letter(isMatchedYellow: false, isMatchedGreen: false, letterText: Text("O")) , count: rows * columns)
}
func indexIsValid(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> Text {
get {
assert(indexIsValid(row: row, column: column), "Index out of range")
return grid[(row * columns) + column].letterText
}
set {
assert(indexIsValid(row: row, column: column), "Index out of range")
grid[(row * columns) + column].letterText = newValue
}
}
}
…
and the code I have in the button:
…
Button(action: {
var thisLetter = letterGrid[0,0]
thisLetter = Text("Q")
renderView()
}, label: {
Text("Letter")
…