Skip to content

Models Configuration

kevindelord edited this page Jun 28, 2016 · 7 revisions

The model configuration is done like in any other CoreData project.

  1. Create and configure your model entities inside one .xcdatamodel or .xcdatamodeld.
  2. Generate the NSManagedObject subclasses as you are used to.

Since Xcode 7.2, two files are generated for each class, one for the attributes, one for the additional functionalities.

Example in Swift: Plane.swift and Plane+CoreDataProperties.swift

Plane+CoreDataProperties.swift

extension Plane {

    @NSManaged var destination: String?
    @NSManaged var origin: String?
    @NSManaged var passengers: Passenger?

}

Plane.swift

class Plane: NSManagedObject {

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

}

Warning

If the generated classes are written in Obj-C remember to import the .h files in the bridge-header file or .pch.

Optional

Add the following optional functions into the model class:

Override to improve how the receiving entity is described/logged.

override var description : String {
    return "{  origin: \(self.origin), destination: \(self.destination) }"
}

Override to specify a default order for the + all and + count functions.

override class func sortingAttributeName() -> String? {
    return "origin"
}

Override to toggle the log for the receiving class.

If this value returns true and if the DKDBManager.verbose == true then the manager will automatically print the entities for this class during the CRUD process. The number of objects and their (overriden) description will also be logged.

override class func verbose() -> Bool {
	return true
}

Another very handy feature, even the activity around this class model will be printed:

Updating Plane { origin: "Berlin", destination: "Paris }