I have declared an array as a State var
@State var proba : [Int] = [10, 40, 40, 10]
The array is updated in onAppear :
.onAppear {
proba = arrayOfTuples.map { (proba, _) in proba }
print("proba", proba)
}
The array is used for TextFields:
ForEach(0..<proba.count, id: \.self) { index in
TextField("N", value: $proba[index], format: .number)
.frame(width: 40)
.onChange(of: proba_kWh[index], perform: {[oldValue = proba_kWh[index]]
newValue in
print("proba onChange", index, oldValue, proba[index], newValue)
})
When View appears, I get some onChange (as initial values did change)
proba [10, 20, 60, 10] // onAppear changes 2 values: 1 and 2
proba onChange 2 40 60 60 // index 2 is changed before 1
proba onChange 1 40 20 20
I know that order of execution is not guaranteed, but in this specific case, I did thought it would be in the order of the array reading…