xcode 7 generates core data entity with additional CoreDataProperties category

Yevgeniy Logachev picture Yevgeniy Logachev · Oct 13, 2015 · Viewed 11.3k times · Source

I have strange new feature in xcode 7, when I generate new NSManagedObject subclass then xcode create two classes: entity and their CoreDataProperties category, which contain full implementation. In picture below an example what I mean.

enter image description here

I cannot find any documented info about this, who can explain why it works so

Answer

Leszek Szary picture Leszek Szary · Oct 16, 2015

I just noticed this and also could not find any documentation about it but I've experimented with this new feature and it works like this. When you first generate NSManagedObject subclass from your Core Data model then Xcode will generate 4 files:

DBUser.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

NS_ASSUME_NONNULL_BEGIN

@interface DBUser : NSManagedObject

// Insert code here to declare functionality of your managed object subclass

@end

NS_ASSUME_NONNULL_END

#import "DBUser+CoreDataProperties.h"

DBUser.m

#import "DBUser.h"

@implementation DBUser

// Insert code here to add functionality to your managed object subclass

@end

DBUser+CoreDataProperties.h

#import "DBUser.h"

NS_ASSUME_NONNULL_BEGIN

@interface DBUser (CoreDataProperties)

@property (nullable, nonatomic, retain) NSNumber *id;
@property (nullable, nonatomic, retain) NSString *name;

@end

NS_ASSUME_NONNULL_END

DBUser+CoreDataProperties.m

#import "DBUser+CoreDataProperties.h"

@implementation DBUser (CoreDataProperties)

@dynamic id;
@dynamic name;

@end

So as you can see now all properties are in a separate file with category (CoreDataProperties). Later if you generate NSManagedObject subclass for the same model Xcode 7 will regenarete only 2 files with category (DBUser+CoreDataProperties.h and DBUser+CoreDataProperties.m) to update all properties from your model but it will not make any changes to 2 other files (DBUser.h and DBUser.m) so you can use these 2 files to add there some custom methods or properties etc.

In previous version Xcode generated always only 2 files (DBUser.h and DBUser.m) and it put properties there so you could not easily modify these files because your custom implementation was deleted everytime you regenerated your subclasses. Therefore it was a common practice to manually create a category and put your methods in your category which was oposite to what we can see in Xcode 7. That however had many disadvantages because we had to use a category for implementation of our methods which does not allow to do certain things and now we can easily modify the main interface and implementation files which allows us to do anything with it. Hurray!