Article

Generating Code

Automatically or manually generate managed object subclasses from entities.

Overview

After you define your entities, their attributes, and relationships as described in Configuring a Core Data Model, specify the classes that you’ll use to create instances of your entities. Core Data optionally generates two files to support your class: a class file and a properties file.

The class file declares the class as a subclass of NSManagedObject:

//
//  Quake+CoreDataClass.swift
//  
//  This file was automatically generated and should not be edited.
//

import Foundation
import CoreData

@objc(Quake)
public class Quake: NSManagedObject {

}

The properties file declares an extension to hold the @NSManaged properties representing attributes and relationships, their accessors, and helper functionality for fetching instances of this type.

//
//  Quake+CoreDataProperties.swift
//
//  This file was automatically generated and should not be edited.
//

import Foundation
import CoreData

extension Quake {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Quake> {
        return NSFetchRequest<Quake>(entityName: "Quake")
    }

    @NSManaged public var magnitude: Float
    @NSManaged public var place: String?
    @NSManaged public var time: Date?
    @NSManaged public var countries: NSSet?

}

// MARK: Generated accessors for countries
extension Quake {

    @objc(addCountriesObject:)
    @NSManaged public func addToCountries(_ value: Country)

    @objc(removeCountriesObject:)
    @NSManaged public func removeFromCountries(_ value: Country)

    @objc(addCountries:)
    @NSManaged public func addToCountries(_ values: NSSet)

    @objc(removeCountries:)
    @NSManaged public func removeFromCountries(_ values: NSSet)

}

Core Data takes care of generating managed object subclasses for you, but you take control when you need to add logic or edit properties.

Select a Code Generation Option

To select a code generation option:

  1. Select an entity from the Entities list.

  2. In the Data Model inspector, under Class, the Codegen list offers three options: Manual/None, Class Definition, and Category/Extension.

Screenshot showing the .xcdatamodeld file with an entity selected in the Entities list at left, and the Codegen options in the Data model inspector at right.

The sections that follow describe circumstances for choosing each option.

Choose Class Definition If You Won't Need to Edit the Generated Logic or Properties

Choose this option when you don’t need to edit the properties or functionality of the managed object subclass and properties files that Core Data generates for you.

The generated source code doesn’t appear in your project’s source list. Xcode produces the class and properties files as part of the build process and places them in your project’s build directory.

These files regenerate whenever the related entity changes in the data model.

Choose Category/Extension to Add Logic to the Managed Object Subclass

Choose this option to add additional convenience methods or business logic inside your managed object subclass.

With the Category/Extension option you take full control of the class file, while continuing to automatically generate the properties file to keep it up-to-date with the model editor. It’s up to you to create and maintain your class manually.

To generate the class file initially:

  1. Choose Editor > Create NSManagedObject Subclass from the menu at the top of the screen.

  2. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.

  3. Because the properties file continues to be produced as part of the build process, move this duplicate file from your project source list to the trash.

Screenshot of Editor > Create NSManagedObject Subclass menu.

You can now see and edit the class file in your project source list.

Screenshot showing a Quake+CoreDataClass.swift file in the project navigator.

Choose Manual/None to Edit Properties and Logic in the Managed Object Subclass

Choose this option to edit the properties in your managed object subclass, for example, to alter access modifiers, and to add additional convenience methods or business logic.

With Manual/None, Core Data doesn’t generate files to support your class. You create and maintain your class, including its properties, manually. Core Data then locates these files using the values you supply in the class name and module fields.

To generate the class and properties files initially:

  1. Choose Editor > Create NSManagedObject Subclass from the menu at the top of the screen.

  2. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both a class and a properties file into your project.

Screenshot of Editor > Create NSManagedObject Subclass menu.

You can now see and edit both the class and properties files in your project source list.

Screenshot showing Quake+CoreDataClass.swift and Quake+CoreDataProperties.swift files in the project navigator.

See Also

Configuring a Core Data Model

Configuring Entities

Model your app’s objects.

Configuring Attributes

Describe the properties that compose an entity.

Configuring Relationships

Specify how entities relate and how change propagates between them.