I have some lengthy code with explicit colors, that works . I have reduced the cogent parts to the simplification below . My View Model is passed to my view as an environment object.
struct ConteView: View {
@EnvironmentObject var game: colorGameVM
...
// a bunch of @State vars
...
@State var angGrad: [Color] = [Color.red,Color.white,Color.blue]
var body: some View {
// bunch of stuff
}
}
I want to dynamically use colors from my View Model, so I changed it like this
struct ConteView: View {
@EnvironmentObject var game: colorGameVM
...
// a bunch of @State vars
...
@State var angGrad: [Color] = [game.color1,game.color2,game.color3]
var body: some View {
// bunch of stuff
}
}
and got the "Cannot use instance member 'game' within property initializer; property initializers run before 'self' is available" error on the angGrad line.
I left the @State type def and moved angGrad code into an initializer but get an error "Variable 'self.manager' used before being initialized" in the init. I don't understand what this means. I cannot bring the game var into the init because as an Environment Object it's a get only property.
struct ConteView: View {
@EnvironmentObject var game: colorGameVM
...
// a bunch of @State vars
...
@State var angGrad: [Color]
init() {
self.angGrad = [game.color1,game.color2,game.color3]
}
var body: some View {
// bunch of stuff
}
}
Any suggestions on how to achieve this? BTW, I know I can access the game.color directly in my View, the angGrad is a an array used for a background color gradient.