Gagan_iOS
Gagan_iOS

Reputation: 4060

CoreData classes in Xcode 7

I have a query from XCode 7 coredata. Suppose I am creating a subclass of NSManagedObject (let name as "NoteEntity"), now I have two classes generated by Xcode named as "NoteEntity+CoreDataProperties.swift" and "NoteEntity.swift". I know that first one is an extension like category in objective c.

My question is that why Xcode is generating two classes? In what way we can use these two classes for more productivity?

Upvotes: 1

Views: 289

Answers (2)

dosdos
dosdos

Reputation: 1519

Two classes are generated, but only one will be re-generated (NoteEntity+CoreDataProperties.swift) if you change your model later.

Thanks to that new feature, you will be able to add some methods or even attributes on the NoteEntity.swift class. And they won't be erased by a new generation of your model !

Before that feature, we had to create our own category to do so.

It is also even more better than before considering this : You have two classes; Message and Comment (which is a subclass of Message). Before that, if you had a category on Message and Comment, and needed to use methods inside these two categories, you would have to include these two class. But now, you'll just have to include Comment.swift, which include the category (because it's kinda category) Message.swift :)

Upvotes: 2

ajpallares
ajpallares

Reputation: 787

I don't think it's about productivity.

When you update your entity (add/remove attributes or relationships), then the file NoteEntity+CoreDataProperties.swift will be regenerated (and therefore, any code there will be deleted). That's why you should do the implementation of the class in the file NoteEntity.swift. This way, it won't be deleted every time you update your NSManagedObject subclases.

Upvotes: 1

Related Questions