core data in a static library for the iPhone

后端 未结 8 1218
不知归路
不知归路 2020-11-28 02:07

I\'ve built a static library that makes heavy use of the Core Data framework. I can successfully use the library in my external project, but ONLY if I include the .xcdatamod

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 02:18

    Swift 2 version for Sascha's answer:

    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.
        var allBundles = NSMutableSet()
        allBundles.addObjectsFromArray(NSBundle.allBundles())
        allBundles.addObjectsFromArray(NSBundle.allFrameworks())
    
        let model =  NSManagedObjectModel.mergedModelFromBundles(allBundles.allObjects as? [NSBundle])
    
        return model!
    }()
    

提交回复
热议问题