Swift 2.0 One-to-Many Relationships Still an NSOrderedSet?

Is there a one-to-many relationship in Swift 2.0 in Core Data that is still NSOrderedSets?

It seems like I can't use cast / downcast to never throw errors.

Does anyone have a simple example of this in Swift 2.0?

+8
ios swift core-data nsorderedset
source share
2 answers

Unfortunately, CoreData with fast speed is still a huge pain, especially with streamlined one-to-many relationships. Yes, this is still an NSOrderedSet that is used by CoreData relationships for many. The question is how to add / remove items. Xcode has never been able to generate accessories properly for an ordered set so far - even in Objective, not to mention Swift !!.

Swift has this thread for preservation in many ways: How to define CoreData relationships in Swift?

But alas! Nothing is mentioned in this thread ever working in the Swift 2.0 world. So what is the workaround right now? I delved into this, and the only way to get this working is to generate sources for the objects in question in Objective-C, rather than quickly and exporting the headers from them to the bridge header. You also need to make sure that you include an important fix for Xcode to create the right accessories for your ordered set:

https://github.com/CFKevinRef/KCOrderedAccessorFix

You need to make sure model.kc_generateOrderedSetAccessors () in the model creation code in AppDelegate to trigger this fix.

Once you're done, you can now safely start using the generated accessors on your model to add elements to many.

I created a sample project and is on github and hope this helps -

https://github.com/shripada/CoreDataToManyRelationshipInSwift2.0

+1
source share

Only if you check the link with the "Ordered" option. If not, then this is a NSSet .

I do not agree with the accepted answer, because in Swift 2.0 the bridges from NSSet to Set happen automatically, so if you can subclass the CoreData object, you can simply write:

 @NSManaged var oneToManyOfTypeFoo: Set<Foo> 

And then adding and removing elements becomes trivial because you can just use the insert and remove methods from Set .

+4
source share

All Articles