Error: "Cannot use instance member 'colors' within property initializer; property initializers run before 'self' is available"

(ok, I fixed this by moving the colorProvider ownership out to the caller.)

    struct QuipView: View {
        let date: Date
        let  colors: [Color]
        @StateObject var colorProvider = ColorProvider(colors: colors)  // <== Cannot use instance member 'colors' within property initializer; property initializers run before 'self' is available

        var body: some View {
            colorProvider.color
                .onChange(of: date) { _ in
                    colorProvider.advance()
                }
        }
    }

How to fix this?

init(date: Date, colors: [Color]) {
  self.date = date
  _colorProvider = .init(wrappedValue: .init(colors: colors))
}
1 Like

Thank you! This solved my problem and allow me to keep the good structuring. I keep forgetting this trick. Wish the compiler can give out more helpful error message.