Logo for tanaschita.com

How to create NSManagedObject subsclasses for Core Data entities in Xcode

Learn about Xcode's code generation options when creating Core Data models in Swift.

24 Oct 2022 · 3 min read

As described in this developer guide on the main aspects of Core Data, each entity we setup in the .xcdatamodeld file describes an object and has a corresponding class that inherits from NSManagedObject.

Xcode provides three different code generation options to control how those subclasses are created.

Xcode's entity codegen options.
Xcode's entity codegen options.

As we can see above, the options are Manual/None, Class Definition and Category/Extension. We can choose which option to use for a specific entity in the Data Model inspector by selecting the entity in the .xcdatamodeld file.

Let's take a closer look at what each of that options means.

Sponsorship logo
Join the FREE iOS Architect Crash Course (for a limited time!)
If you’re a mid/senior iOS developer who’s looking to improve both your skills and salary level, then join this 100% free online crash course. Hurry up because it's available only until November 27th!
Click to get it now

Codegen option Class Definition

This is the default codegen option when we add a new entity. With the Class Definition option selected, Core Data generates an NSManagedObject subclass with all entity properties and relationships automatically.

For example, a generated subclass for a User entity looks as follows:

User+CoreDataClass.swift

@objc(User)
public class User: NSManagedObject {
}

User+CoreDataProperties.swift

extension User {
@nonobjc public class func fetchRequest() -> NSFetchRequest<User> {
return NSFetchRequest<User>(entityName: "User")
}
@NSManaged public var username: String?
@NSManaged public var lastLogin: Date?
@NSManaged public var verified: Bool
@NSManaged public var posts: NSSet?
}
extension User {
@objc(addPostsObject:)
@NSManaged public func addToPosts(_ value: Post)
@objc(removePostsObject:)
@NSManaged public func removeFromPosts(_ value: Post)
@objc(addPosts:)
@NSManaged public func addToPosts(_ values: NSSet)
@objc(removePosts:)
@NSManaged public func removeFromPosts(_ values: NSSet)
}

As we can see above, Core Data creates a class file and an extension file. These files don't appear in our source code. Xcode produces them as part of the build process and places them in the projects build directory. After building the project, we are able to reference the User entity in our source code. The files are regenerated every time we change the corresponding entity.

Note: If Xcode throws a compiler error saying Cannot find type 'User' in scope, try to clean build the project.

Codegen option Category/Extension

With the Category/Extension option selected, Xcode only generates the properties file User+CoreDataProperties.swift.

We are responsible to generate the class file ourselves which gives us more control for example to add additional methods or business logic inside the managed object subclass.

To generate the class file initially, we can use Xcode's help by selecting Editor > Create NSManagedObject Subclass in the menu.

Codegen option Manual/None

When using the Manual/None option, Core Data doesn't generate any files. We create and maintain the subclass and all its properties and relationsships ourselves.

Core Data locates these files using the values we supply in the class name and module fields in Xcode's Data Model inspector when selecting an entity in the .xcdatamodeld file.

To generate a class and properties file initially, we can use Xcode's help by selecting Editor > Create NSManagedObject Subclass in the menu.

Conclusion

The decision whether to let Xcode automatically generate entity subclasses and their properties for us or to create and manage them manually depends on individual project needs and developer preferences.

The automatic generation option has the advantage that we don't have to worry about keeping the entity classes up-to-date, Xcode will do that for us.

The manual option on the other hand provides us with more control and flexibility, for example to improve the handling of non-optional properties. If you are interested to learn more, checkoout this article on how to handle non-optional Core Data properties in Swift.

Sponsorship logo
Join the FREE iOS Architect Crash Course (for a limited time!)
If you’re a mid/senior iOS developer who’s looking to improve both your skills and salary level, then join this 100% free online crash course. Hurry up because it's available only until November 27th!
Click to get it now

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

core data

persistence

xcode

ios

How to handle non-optional Core Data properties in Swift

Explore the possibilities to use non-optional Swift values in Core Data.

07 Nov 2022 · 4 min read

Latest articles and tips

© 2022 tanaschita.com

Privacy policy

Impressum