Core data: Failed to load model

后端 未结 5 1460
渐次进展
渐次进展 2020-12-15 03:54

I am new to core data.

What I am trying to DO: I am trying to create a cocoatouch framework that has an app to add employee details and display them

5条回答
  •  无人及你
    2020-12-15 04:25

    Explicitly pass the models file name to the Core Data stack for initialization and make sure, it is loaded from the right bundle at the time (test bundle, app bundle...) by using Bundle(for: type(of: self)):

    //...
    let momdName = "SimpleFramework" //pass this as a parameter
    //...
    
    guard let modelURL = Bundle(for: type(of: self)).url(forResource: momdName, withExtension:"momd") else {
            fatalError("Error loading model from bundle")
    }
    
    guard let mom = NSManagedObjectModel(contentsOf: modelURL) else {
        fatalError("Error initializing mom from: \(modelURL)")
    }
    
    persistentContainer = NSPersistentContainer(name: momdName, managedObjectModel: mom)
    
    //...
    

    Edit:

    Also make sure, the SimpleFramework.xcdatamodeld is added to the used targets Target Membership:

提交回复
热议问题