Core data: Failed to load model

后端 未结 5 1458
渐次进展
渐次进展 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:19

    The string you pass to the NSPersistentContainer initializer:

    NSPersistentContainer(name: "CoreData")
    

    needs to match the filename of the data model file in your Xcode project:

    CoreData.xcdatamodeld
    
    0 讨论(0)
  • 2020-12-15 04:24

    In my case, for some reason the DataModel.xcdatamodeld became missing from my project workspace.

    First I tried creating a new DataModle.xcdatamodeld and recreating the data model, but the same error occurred. Thats when I realized that the Original DataModel.xcdatamodeld was still in the root directory. I fixed this by simply right clicking my project in my project navigator, and selecting "Add files to "Project"...", then I added my old data model and deleted my new data model. Finally I hard cleaned, ran my project and it fixed the issue.

    0 讨论(0)
  • 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:

    0 讨论(0)
  • 2020-12-15 04:25

    My problem was at my .podspec file. You should include the xcdatamodeld extension on the pod that you are creating.

    s.resources = "myprojectfolder/**/*.{png,jpeg,jpg,storyboard,xib,xcassets,xcdatamodeld}"

    0 讨论(0)
  • 2020-12-15 04:28

    I've had this issue, when I had wrong model name - it should be models name, not the projects (see the screen shot)

    0 讨论(0)
提交回复
热议问题