无法将类型“(Store).Type”的值转换为预期的参数类型 'Binding<C>'
Cannot convert value of type '(Store).Type' to expected argument type 'Binding<C>'
我正在尝试将项目添加到 1 个视图中的数组,然后使用 foreach 循环在另一个视图中显示该数组,但我不断收到这些错误
Cannot convert value of type '(Store).Type' to expected argument type 'Binding<C>'
和Generic parameter 'C' could not be inferred
这是我的 class - 导入 SwiftUI
class StoreViewModel: ObservableObject{
@Published var item: [String] = []
@Published var amount: [String] = []
}
这里是我尝试显示添加的项目的地方
import SwiftUI
struct Store: View {
@State var presented: Bool = false
@State var showingAlert:Bool = false
@StateObject var store = StoreViewModel()
var body: some View {
ScrollView{
VStack{
HStack{
Button {
showingAlert = true
} label: {
Image(systemName: "trash")
.foregroundColor(Color.red)
.font(.system(size: 20))
}
.alert("Are you sure you want to remove all items", isPresented: $showingAlert) {
Button("OK", role: .destructive) {
withAnimation(.easeOut(duration: 0.3)){
}
}
}
Text("Your Cupboard")
.font(.system(size: 30))
.fontWeight(.semibold)
.padding()
Spacer()
Button {
presented.toggle()
} label: {
Image(systemName: "plus")
.font(.system(size: 30))
.foregroundColor(Color(hex: "FF0044"))
}
.fullScreenCover(isPresented: $presented, content: AddItem.init)
}
.padding(.horizontal)
Text("Here you can see what you have in your cupboard")
.padding()
.multilineTextAlignment(.center)
Text("Click the + to add an item")
.padding()
ForEach((Store), id: \.self){item in
HStack{
Spacer()
Text(item)
.font(.system(size: 20))
Text("30g")
Spacer()
Button {
} label: {
Image(systemName: "trash")
.foregroundColor(.red)
.font(.system(size: 15))
}
.padding(.trailing)
}
.padding()
}
}
}
.environmentObject(store)
}
}
我做错了什么,我该如何克服错误?
非常感谢您的时间和帮助
区分大小写很重要。您的意思是 store
,StoreViewModel
实例,而不是 self
的类型。此外,ForEach
表达式的目的是迭代数组(大概是 item(s)
)。
首先为了避免混淆,将 StoreViewModel
中的 item
重命名为复数形式
class StoreViewModel: ObservableObject{
@Published var items: [String] = []
@Published var amount: [String] = []
}
然后替换
ForEach((Store), id: \.self){ item in
和
ForEach(store.items, id: \.self){ item in
我正在尝试将项目添加到 1 个视图中的数组,然后使用 foreach 循环在另一个视图中显示该数组,但我不断收到这些错误
Cannot convert value of type '(Store).Type' to expected argument type 'Binding<C>'
和Generic parameter 'C' could not be inferred
这是我的 class - 导入 SwiftUI
class StoreViewModel: ObservableObject{
@Published var item: [String] = []
@Published var amount: [String] = []
}
这里是我尝试显示添加的项目的地方
import SwiftUI
struct Store: View {
@State var presented: Bool = false
@State var showingAlert:Bool = false
@StateObject var store = StoreViewModel()
var body: some View {
ScrollView{
VStack{
HStack{
Button {
showingAlert = true
} label: {
Image(systemName: "trash")
.foregroundColor(Color.red)
.font(.system(size: 20))
}
.alert("Are you sure you want to remove all items", isPresented: $showingAlert) {
Button("OK", role: .destructive) {
withAnimation(.easeOut(duration: 0.3)){
}
}
}
Text("Your Cupboard")
.font(.system(size: 30))
.fontWeight(.semibold)
.padding()
Spacer()
Button {
presented.toggle()
} label: {
Image(systemName: "plus")
.font(.system(size: 30))
.foregroundColor(Color(hex: "FF0044"))
}
.fullScreenCover(isPresented: $presented, content: AddItem.init)
}
.padding(.horizontal)
Text("Here you can see what you have in your cupboard")
.padding()
.multilineTextAlignment(.center)
Text("Click the + to add an item")
.padding()
ForEach((Store), id: \.self){item in
HStack{
Spacer()
Text(item)
.font(.system(size: 20))
Text("30g")
Spacer()
Button {
} label: {
Image(systemName: "trash")
.foregroundColor(.red)
.font(.system(size: 15))
}
.padding(.trailing)
}
.padding()
}
}
}
.environmentObject(store)
}
}
我做错了什么,我该如何克服错误? 非常感谢您的时间和帮助
区分大小写很重要。您的意思是 store
,StoreViewModel
实例,而不是 self
的类型。此外,ForEach
表达式的目的是迭代数组(大概是 item(s)
)。
首先为了避免混淆,将 StoreViewModel
中的 item
重命名为复数形式
class StoreViewModel: ObservableObject{
@Published var items: [String] = []
@Published var amount: [String] = []
}
然后替换
ForEach((Store), id: \.self){ item in
和
ForEach(store.items, id: \.self){ item in