10

I generated a CoreData model with some 1-to-many relationships. Now I want to use a ForEach on this relationship which is a NSSet, then I'm getting the following error:

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

My code looks like this:

struct DetailView: View {
    var sample: Sample

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

How to solve this?

gurehbgui
  • 14,236
  • 32
  • 106
  • 178

2 Answers2

9

Here is possible approach

ForEach(Array(sample.stepps! as Set), id: \.self) { step in
    // step is NSObject type, so you'll need it cast to your model
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
0
if let steps = sample.steps?.allObjects as? [Step] {
  ForEach(steps) {step in
    Text("\(step.title ?? "step")")
  }
}
Sifan
  • 9
  • 1