I have a simple SwiftUI view which displays three number positions and a refresh button. The refresh button runs a pickThree() function which replaces each number with a random one.
In order to start the view, I need to give each position a starting number, as shown in the allVals variable.
I want to start the view with a random number in each position, and not the hard-coded numbers from the allVals variable. It seems that I should be able to do this by running the same pickThree() refresh function… perhaps by setting it as the value of the allVals variable upfront:
@State var allVals = pickThree()
This produces an error:
Cannot use instance member 'pickThree' within property initializer; property initializers run before 'self' is available
What am I missing here?
import SwiftUI
struct ThreeCards: View {
// @State var allVals = pickThree() // PRODUCES ERROR
@State var allVals = [1,2,3]
var body: some View {
VStack {
HStack {
Text(String(allVals[0]))
Text(String(allVals[1]))
Text(String(allVals[2]))
}
Button(action: {
pickThree()
}) {
Text("Refresh")
}
}
}
func pickThree() -> [Int] {
let one = Int.random(in: 1...3)
let two = Int.random(in: 1...3)
let three = Int.random(in: 1...3)
allVals = [one, two, three]
return allVals
}
}