foreachswiftuinsset

How to use ForEach on a NSSet generated by CoreData in SwiftUI


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?


Solution

  • 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
    }