Search code examples
swiftcore-dataios9nsmanagedobject

Unable to load momd file in ios 9 iPhone 4 simulator


I am using Core data with iOS 9.0, iPhone4 Simulator & Xcode 9.2.

While loading momd file application crashes with nil value.

Although i already added Address.xcdatamodel file to bundle resource & target membership checkbox selected.

lazy var managedObjectModelForAddress: 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 = Bundle.main.url(forResource: "Address", withExtension: "momd")!//Crashes here.
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()

Solution

  • I was using many .xcdatamodel files some of them get worked with Solution1 & some with solution 2

    Solution 1:

    • Delete App from simulator
    • Delete .xcdatamodel from xcode & save it somewhere, & re add it.
    • Quit Xcode, Delete Derived data & Repoen XCODE.

    Solution 2:

    • Delete App from simulator

    • Create new .xcdatamodel & copy all contains to new .xcdatamodel file

    • Quit Xcode, Delete Derived data & Repoen XCODE.