【问题标题】:Cannot convert value of type 'Binding<_>' to expected argument type 'Binding<Card>'无法将“Binding<_>”类型的值转换为预期的参数类型“Binding<Card>”
【发布时间】:2021-09-29 03:31:19
【问题描述】:

我正在尝试创建与 FetchedResults 项目的绑定,$items[i] 上出现错误:

struct NavView: View {   
    @Binding var item : Card

    ...
}

struct ContentView: View {
private var items: FetchedResults<Card>

var body: some View {
    List {
        ForEach(items.indices, id:\.self) { i in
            NavigationLink {
                NavView(item: $items[i])
            }
        }
    }
}
}

【问题讨论】:

  • 这真的是一个您无法通过运行简单的搜索引擎搜索找到答案的主题吗?
  • 我在 Google 中找到的最接近的链接是 stackoverflow.com/questions/58724301/…,它似乎并没有真正回答这个问题

标签: ios swift swiftui-navigationview


【解决方案1】:

将绑定更改为 ObservedObject 编译并且似乎工作正常,尽管我觉得我通过创建一个新的 ObservedObject 违反了单一事实来源策略。

struct NavView: View {   
    @ObservedObject var item : Card

    ...
}

struct ContentView: View {
private var items: FetchedResults<Card>

var body: some View {
    List {
        ForEach(items) { item in
            NavigationLink {
                NavView(item: item)
            }
        }
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2020-12-30
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    相关资源
    最近更新 更多