xcode 7 generates core data entity with additional CoreDataProperties category xcode 7 generates core data entity with additional CoreDataProperties category xcode xcode

xcode 7 generates core data entity with additional CoreDataProperties category


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@endNS_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;@endNS_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!


Previously the generated code went into a EntityName.h and EntityName.m that you had to "extend" with an interface such as EntityName+Create.h and EntityName+Create.m.

This was hard to understand for beginners who often modified the EntityName.m class and lost their code.

Now it is in the right way: the code generator will not erase the existing code.

The other answers are very good at explaining the new system.

But nobody talks about the new compatibility issue if you have entities based on the old system.

My solution: I still put my own code in EntityName+Create.m, but in EntityName+Create.h I refer to EntityName+CoreDataProperties.h instead of just EntityName.h (I emptied the previously generated code in EntityName.h and EntityName.m). This solution avoided me to move my code from EntityName+Create.m and to change all references to EntityName+Create.h.

I hope this helped you.


For Swift and Xcode 7 it is the same as the answer from Leszek S but only 2 files are created initially:

  • DBUser.swift
  • DBUser+CoreDataProperties.swift

Later, if you make changes to the CoreData model and regenerate the NSManagedObject subclass only the DBUser+CoreDataProperties.swift gets updated. DBUser.swift is left untouched.

So put all your code in DBUser.Swift.