【问题标题】:How to use ForEach on a NSSet generated by CoreData in SwiftUI如何在 SwiftUI 中 CoreData 生成的 NSSet 上使用 ForEach
【发布时间】:2020-07-09 01:26:18
【问题描述】:

我生成了一个包含一对多关系的 CoreData 模型。现在我想在 NSSet 这个关系上使用 ForEach,然后我收到以下错误:

Generic struct 'ForEach' requires that 'NSSet' conform to 'RandomAccessCollection'

我的代码如下所示:

struct DetailView: View {
    var sample: Sample

    var body: some View {
        VStack {
            ForEach(sample.stepps!, id: \.self) { step in
                ...
            }
        }
    }
}

如何解决?

【问题讨论】:

  • 您确定要ForEach 而不是forEach
  • forEach 在 SwiftUI 中不起作用

标签: foreach swiftui nsset


【解决方案1】:

这是可能的方法

ForEach(Array(sample.stepps! as Set), id: \.self) { step in
    // step is NSObject type, so you'll need it cast to your model
}

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多