Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'weak' may only be applied to class and class-bound protocol types, not 'ContentView' what im missing?

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())
    }
}
like image 393
Damiano Miazzi Avatar asked Sep 05 '25 03:09

Damiano Miazzi


2 Answers

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.

like image 129
Asperi Avatar answered Sep 07 '25 20:09

Asperi


Remove [weak self] as it used for refrence types like classes while ContentView is a value type struct

like image 28
Sh_Khan Avatar answered Sep 07 '25 20:09

Sh_Khan