-1

The OrderedSet in swift-collections has many desirable properties. But I can't quite figure out how to, or whether it is possible to, use it to replace the NSOrderedSet to represent a relationship in CoreData entities.

Consider the following entity:

@objc(Ingredient)
class Ingredient: Entity {
    @NSManaged var recipes: NSOrderedSet
}

The code won't compile if I change it to use OrderedSet like the following

import OrderedCollections
class Ingredient: Entity {
    @NSManaged var recipes: OrderedSet<Recipe>
}

The error message is error build: Property cannot be marked @NSManaged because its type cannot be represented in Objective-C.

Alex Dong
  • 975
  • 2
  • 9
  • 18

1 Answers1

2

But I can't quite figure out how to use it to replace the NSOrderedSet to represent a relationship in CoreData entities.

Because it can't be done. Swift OrderedSet is totally unrelated to Cocoa / Objective-C NSOrderedSet. The former is so you don't have to use the latter, but you are talking to Core Data so you do have to use the latter.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I suppose it might be possible write a reducer but it seems hardly worth the trouble of thinking about, even. – matt Jul 25 '22 at 19:37
  • Hey @Matt, thank you for the quick response. That makes sense. I was just wondering if I have missed something. – Alex Dong Jul 25 '22 at 19:47
  • 1
    Yeah, you probably think that because there is Set/NSSet there should be OrderedSet/NSOrderedSet. But there isn't. OrderedSet is not a "front" for NSOrderedSet, like Set/NSSet or Date/NSDate; it's a totally independent being. – matt Jul 25 '22 at 20:05