Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 267
gurehbgui Avatar asked Mar 28 '20 11:03

gurehbgui


1 Answers

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
}
like image 163
Asperi Avatar answered Nov 19 '22 23:11

Asperi