I have the following code:
import SwiftUI
import QGrid
struct VerCanales: View {
var categoria:String
@ObservedObject var canales = CargarCanales(categoriaID: categoria)
var body: some View {
NavigationView{
QGrid(self.canales.listaCanales, columns: 3){item in
NavigationLink(destination:VlcPlayerDemo()){
Text(item.name).frame(width:400, height:50)
}
}
}.navigationBarTitle(Text("CHANNELS AVAILABLE"))
}
}
The variable "category" comes from the above view as a parameter and I need to pass it as a parameter to the @ObservedObject but I get the following error: "Cannot use instance member 'category' within property initializer; property initializers run before 'self' is available"
I guess you mean that I can't use the variable since it hasn't been initialized.
I am new to the language and have searched but can't find a good solution
I have solved it in a simpler way in this way:
When working with
@ObservedObject
you need to initialize the actual value of the object before using.In your class just the
init
like this:You have to initialize the object in the view and save the object for use: