[英]ObservedObject not convertible to Binding
I'm trying to implement MVVM with swiftUI我正在尝试使用 swiftUI 实现 MVVM
So I have this view model所以我有这个视图模型
class HomeViewModel: ObservableObject {
@Published var favoriteStores = Array<ItemStore>()
init() {
for i in 0...10 {
favoriteStores.append(ItemStore(storeName: "Store \(i)", storeImg: "image url"))
}
}
}
And this view :这个观点:
struct HomeView: View {
@ObservedObject var homeVM = HomeViewModel()
@State var faves = [
ItemStore(storeName: "Store 0", storeImg: "image url"),
ItemStore(storeName: "Store 1", storeImg: "image url"),
ItemStore(storeName: "Store 2", storeImg: "image url")
]
@State var searchText = ""
var body: some View {
NavigationView{
GeometryReader { geometry in
ScrollView{
VStack{
SearchBarView(searchText: self.$searchText)
Spacer()
.padding(.vertical, 5.0)
FavoriteStoresView(favoriteStores: self.homeVM.favoriteStores)
FiltersView()
StoresView()
}.padding()
}
}
}
}
}
the problem here is when i use self.homeVM.favoriteStores
i got : '[ItemStore]' is not convertible to 'Binding<[ItemStore]>'
这里的问题是,当我使用
self.homeVM.favoriteStores
我得到了: '[ItemStore]' is not convertible to 'Binding<[ItemStore]>'
but when i use @State var faves
instead , it works fine但是当我使用
@State var faves
,它工作正常
i saw lot of tutorials , and it should work like that , because swiftUI handle this part , and it wrap it with Binding我看过很多教程,它应该像那样工作,因为 swiftUI 处理这部分,并用Binding包装它
Change the line with FavoriteStoresView
to:将带有
FavoriteStoresView
的行更改为:
FavoriteStoresView(favoriteStores: self.$homeVM.favoriteStores)
(add a $
before the member var) (在成员 var 前加一个
$
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.