A naive question that I'll ask naively. If I have an array:
var testArray = [Double](repeating: 0.0, count: 100)
And want to populate it, in this instance with some dummy data:
func populateTestArray() {
ForEach((0 ..< testArray.count), id: \.self) { index in
testArray[index] = Double.random(in: 1...100)
}
}
Where, and how, do I do so?
The compiler, quite understandably, says that testArray is not in the scope. What is the approved way of doing this in a .swift? I don't want to have to resort to Global Variables as this data isn't.
I have tried defining the variable with @State and with a .onAppear in the var body: some View { but again the compiler says "no".
ForEach((0 ..< testArray.count), id: \.self) { index in
testArray[index] = Double.random(in: 1...100)
}.onAppear
What is the approved approach?