Adding the module name to the main data objects

I am having trouble developing the implications of this note from Apple "Using Swift with Cocoa and Objective-C" :

Swift classes are namespaced-theyre bound to the module (usually the project) in which they are compiled. To subclass a Swift class NSManagedObject

with your Core Data model, prefix the class name in the Class field to the Object Model inspector with the name of your module.

I did this using my own application and only the stock master detail template, so my entity name is "Event" and its class is "Stock_Master_Detail.Event". When I select Create NSManagedObject Subclass

from the menu Editor

and ask him to subclass Swift, he doesn't call the class correct. Xcode creates a named "Stock_Master_Detail.swift" file for the named class Stock_Master_Detail

. And if I create multiple entities, all prefixed with the module name, Xcode cannot generate more than one subclass as they will all have the same name.

I'll add that everything works fine in my limited testing if I just omit the module name entirely, which is contrary to Apple's documentation. So my question is, what are the consequences of not adding the module name to my class?

+3


source to share


2 answers


I believe it might be a bug with Swift / Core data. First create a managed object with no module name (prefix). After creating it, go back and add the prefix to the master data.



0


source


One way to get around this is to leave the data model file alone, so in this case the class would be "Event". Then in the code where the NSManagedObjectModel is created (AppDelegate if you used the Apple project template), I changed it to:

lazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = NSBundle(forClass: self.classForCoder).URLForResource("Model", withExtension: "momd")!
    var bundleModel = NSManagedObjectModel(contentsOfURL: modelURL)!

    var objectModel = NSManagedObjectModel()
    objectModel.entities = bundleModel.entities.map { ( entity ) -> NSEntityDescription in
        var entityCopy = entity.copy() as NSEntityDescription
        entityCopy.managedObjectClassName = "Stock_Master_Detail." + entity.managedObjectClassName
        return entityCopy
    }

    return objectModel
    }()

      



One improvement, I would like to figure out how to find the module name dynamically. I know I can parse the current class name, but that seems weirder than I want.

0


source







All Articles