我对Swift和开发都是100%全新的。我已经读了几本书,并且开始了解它,但是我可能犯了一些愚蠢的错误。
问题是我有一个列表,其中显示了Measurements()的集合,并且我试图获取NavigationLink,因此当发生触摸时,EditMeasurementView将打开。我不确定自己在做什么错。
这是EditMeasurementView:
struct EditMeasurementView: View {
@Binding var measurements: Measurements
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
}
struct EditMeasurementView_Previews: PreviewProvider {
static var previews: some View {
//EditChecklistItemView(checklistItem: .constant(ChecklistItem(name: "Sample item")))
EditMeasurementView(measurements: .constant(Measurements()))
}
}
这是ContentView:
import SwiftUI
struct ContentView: View {
@ObservedObject var measurements = Measurements()
@State var newMeasurementViewIsVisible = false
var body: some View {
NavigationView {
List {
ForEach(measurements.items) { MeasurementItem in
NavigationLink(destination: EditMeasurementView(measurements: $measurements)) {
HStack {
Text("\(MeasurementItem.mdate)")
Spacer()
Text("\(MeasurementItem.weight)" + " lbs")
}
}
}
.onDelete(perform: measurements.deleteListItem)
.onMove(perform: measurements.moveListItem)
}
.navigationBarTitle("Measurements", displayMode: .inline)
.navigationBarItems(
leading: Button(action: { self.newMeasurementViewIsVisible = true
}) {
HStack {
Image(systemName: "plus.circle.fill")
Text("New")
}
},
trailing: EditButton()
)
}
.sheet(isPresented: $newMeasurementViewIsVisible) {
NewMeasurementItemView(measurements: self.measurements)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
迅速引发的错误是:
Cannot convert value of type 'ObservedObject<Measurements>.Wrapper' to
expected argument type 'Binding<Measurements>'" on the NavigationLink
line in ContentView.
答案 0 :(得分:0)
///想通了! ///
在EditMeasurementView中,我绑定到了错误的对象。我应该绑定到MeasurementItem而不是Measurements。 Measurements是包含MeasurementItems的数组,但是这里我只编辑了一组MeasurementItems。