0

我对 Swift 和开发都是 100% 的新手。我已经阅读了几本书,我开始明白了,但我可能犯了一些愚蠢的错误。

问题是我有一个显示测量集()的列表,我正在尝试获取 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()))
    }
}

这是内容视图:

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

swift抛出的错误是:

Cannot convert value of type 'ObservedObject<Measurements>.Wrapper' to
expected argument type 'Binding<Measurements>'" on the NavigationLink
line in ContentView.
4

1 回答 1

0

/// 弄清楚了!///

在 EditMeasurementView 我绑定到错误的对象。我应该绑定到 MeasurementItem 而不是 Measurements。Measurements 是包含MeasurementItems 的数组,但这里我只编辑一组MeasurementItems。

于 2020-01-01T19:51:07.870 回答