14

I'm not sure if I just did not found any information or if it's not possible.

I've looked up several pages. I want to use generic a generic version of the NSOrderedSet in Swift.

With a Set you can do this:

Swift’s Set type is bridged to Foundation’s NSSet class. Source: Apple Docs - Sets

In a NSManagedObject subclass you can simply change the NSSet to Set and it will work. (Tested it)

@property NSArray<NSDate *> *dates;
@property NSSet<NSString *> *words;
@property NSDictionary<NSURL *, NSData *> *cachedData;

var dates: [NSDate]
var words: Set<String>
var cachedData: [NSURL: NSData]

Source: Apple Docs - Lightweight Generics

But I couldn't find any reference to an ordered set in Swift. I could use the NSOrderedSet from Objective-C but I won't have generics in Swift then.

Is there any possibility to define an order in a set in Swift?

2 Answers2

10

No, there is no ordered set in Swift and in all honesty, you shouldn't be using an ordered set period. They are very rarely of use.

But some times, its useful. So you don't have to sort your data by yourself. Especially when you have an entity with a list of sub entities they won't be sorted because it is a set. And then you would have to create a array to have the data prepared in a specific order because a plain set doesn't keep its order. So for core data it is useful especially in use with the NSFetchedResultsController.

It is a lazy addition to Core Data that never should have happened. It is never more useful or more performant than writing your own sort routine. Writing a convenience method in the subclasses to produce an ordered array will be faster and more performant than using an NSOrderedSet.

When you are using an NSFetchedResultsController it is completely useless since the NSFetchedResultsController orders everything for you.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • 2
    But some times, its useful. So you don't have to sort your data by yourself. Especially when you have an entity with a list of sub entities they won't be sorted because it is a set. And then you would have to create a array to have the data prepared in a specific order because a plain set doesn't keep its order. So for core data it is useful especially in use with the NSFetchedResultsController. –  Apr 26 '16 at 08:29
0

I agree with Marcus Zarra's answer, it is generally not a feature that is worth using but wanted to add a couple of things. I would generally recommend adding a property for explicit order vales that you can sort on.

Ordered Sets are second class citizens in Core Data even for Apple, they were not supported in iCloud Core Data stores (although there were issues generally with that system and I believe it is now deprecated generally).

In Swift terms it would be possible to write your own ordered set with value semantics but it isn't possible at this point to enable bridging to and from the Foundation ordered set.

If you were really desperate to support it you can write a custom code generator to create accessors for your custom ordered set type (or just add accessors by hand).

In conclusion there are ways that you could do it but that does not mean that you should.

Joseph Lord
  • 6,446
  • 1
  • 28
  • 32