I'm try to running this code , but i'm getting the warning in the closure... any help to solve it? In order to run a function filter in background thread i been suggest to run the init with the filter function.
But getting this warning in the closure:
'weak' may only be applied to class and class-bound protocol types, not 'ContentView'
import SwiftUI
struct ContentView: View {
@ObservedObject var dm: DataManager
@State private var searchTerm : String = ""
@State var filteredAirports: [AirportModel] = []
init(dataM: DataManager) {
self.dm = dataM
dm.filter(valoreSearhed: searchTerm, arrayTosearh: dm.airportVector, closure: { [weak self] in
self?.filteredAirports = $0 })
}
var body: some View {
VStack {
SearchBar(text: $searchTerm)
List {
ForEach(filteredAirports) { valore in
Text(valore.aptICAO)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(dataM: DataManager())
}
}
weak
is a modifier for reference types (ie. weak pointer, nullable when all references are released). But your ContentView
is a struct
, which is value type.
Remove [weak self]
as it used for refrence types like classes while ContentView
is a value type struct
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With