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

huangapple go评论150阅读模式
英文:

'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.

为了在后台线程中运行筛选函数,建议我在init中运行filter函数。

But getting this warning in the closure:

但在闭包中收到以下警告:

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

'weak'只能应用于类和类绑定的协议类型,而不是'ContentView'。

英文:

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())
    }
}

答案1

得分: 5

"weak"是引用类型的修饰符(即弱引用指针,在所有引用释放时可为空)。但您的ContentView是一个struct,它是值类型。

英文:

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.

答案2

得分: 2

删除[weak self],因为它用于引用类型,比如类,而ContentView是一个值类型struct

英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 17:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定