英文:
'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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论