How to use Core Data's ManagedObjectModel inside a framework?
I'm a bit late for flohei's issue, but hopefully this helps anybody else who wanders by. It is possible to get this to work without having to perform script-fu to copy resources around!
By default Apple's Core Data template gives you something like this:
lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle.mainBundle().URLForResource("MyCoreDataModel", withExtension: "momd")! return NSManagedObjectModel(contentsOfURL: modelURL)!}()
That would load your Core Data resources out of the main bundle. The key thing here is: if the Core Data model is loaded in a framework, the .momd file is in that framework's bundle. So instead we just do this:
lazy var managedObjectModel: NSManagedObjectModel = { let frameworkBundleIdentifier = "com.myorg.myframework" let customKitBundle = NSBundle(identifier: frameworkBundleIdentifier)! let modelURL = customKitBundle.URLForResource("MyCoreDataModel", withExtension: "momd")! return NSManagedObjectModel(contentsOfURL: modelURL)!}()
That should get you up and running.
Credit: https://www.andrewcbancroft.com/2015/08/25/sharing-a-core-data-model-with-a-swift-framework/
You need to drag the xcdatamodeld file and drop it in the Build Phases | Compile Sources for the targets that use the framework. Then when the target runs its [NSBundle mainBundle] will contain the model (momd file).
@Ric Santos was almost there. I think you just need to make it an "mom" extension rather than "momd", then it will run.
lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle(forClass: self.dynamicType.self).URLForResource(self.dataModelName, withExtension: "mom")! return NSManagedObjectModel(contentsOfURL: modelURL)!}()