Skip to content

Instantly share code, notes, and snippets.

@delebedev
Last active March 17, 2017 15:28
Show Gist options
  • Save delebedev/165818fb60f28312096e21462e0aee76 to your computer and use it in GitHub Desktop.
Save delebedev/165818fb60f28312096e21462e0aee76 to your computer and use it in GitHub Desktop.
Swift CoreData craziness
// Class Keyword: NSManagedObject is defined in objective-c (not sure if it matters)
// .keywords is CoreData to-many relationship pointing to "Keyword" objects.
// How do I convert .keywords to [Keyword]?
po type(of: keywords)
_NSFaultingMutableOrderedSet
po keywords is NSOrderedSet
true
// NSOrderedSet does not store type information
po type(of: keywords.array)
Swift.Array<Any>
// well this is expected
po keywords.array is [Keyword]
false
// let's introspect an object
expr let $k = keywords.array[0]
//oh
po $k is Keyword
false
//oh
po type(of: $k)
NSManagedObject_Keyword_
@oleksii-demedetskyi
Copy link

keywords.array as [Keyword] should work. Thanks to magic inside compiler for bridging from objc to swift.

If not working - keywords.array.flatMap { $0 as? Keyword }

@delebedev
Copy link
Author

delebedev commented Mar 17, 2017

Surprise, in whole model only this class had Class Name empty :(. This prevented CoreData magic.

screen shot 2017-03-17 at 15 25 06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment