The reference data model does not appear in the application

I'm having difficulty with Core Data in Xcode 8 beta 1. Old applications will compile and work fine, but all new applications compile and work fine until they try to insert a new NSManagedObject.

Initially, I thought that this was due to the incorrect removal of the old xcdatamodel and the alteration of another, but after creating a completely new application and creating a simple Entity "A", I can not create an object of class A at runtime.

I tried using let a = A(context: myMOC) , which returns an error:

The NSManagedObject of class 'MyApp.A' must have a valid NSEntityDescription.

Trying the old let a = NSEntityDescription.insertNewObject(forEntityName: "A", into: context) as! A let a = NSEntityDescription.insertNewObject(forEntityName: "A", into: context) as! A returns an error:

Failed to distinguish value of type "NSManagedObject_A_" (0x7fd114626f80) to 'MyApp.A' (0x10d2fdf28).

I checked my xcdatamodel a dozen times to make sure everything was spelled correctly and created a new project to check to make sure that I did not make any mistakes when setting up the CD. Thoughts?

UPDATE: Package Contents xcdatamodel:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="11147.23" systemVersion="16A201w" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> <entity name="Coordinate" syncable="YES" codeGenerationType="class"> <attribute name="latitude" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES" syncable="YES"/> <attribute name="longitude" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES" syncable="YES"/> <relationship name="route" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Route" inverseName="coordinates" inverseEntity="Route" syncable="YES"/> </entity> <entity name="Route" syncable="YES" codeGenerationType="class"> <attribute name="uuid" optional="YES" attributeType="String" syncable="YES"/> <relationship name="coordinates" optional="YES" toMany="YES" deletionRule="Nullify" ordered="YES" destinationEntity="Coordinate" inverseName="route" inverseEntity="Coordinate" syncable="YES"/> </entity> <elements> <element name="Route" positionX="-45" positionY="0" width="128" height="75"/> <element name="Coordinate" positionX="-18" positionY="27" width="128" height="90"/> </elements> </model> 

UPDATE 2: Printing managedObjectModel objects shows that the correct model is loading. However, I cannot get NSManagedObject to initialize in any new project.

+8
ios swift xcode8 core-data
source share
3 answers

I finally realized:

In Xcode 7, you will need to enter the class name and in Swift install the module in the "Current project module". I assumed that it would no longer be needed in Xcode 8, as it automatically generates source files. You need to manually set the class name for the name of the generated class, but for Xcode to generate @objc(ClassName) (I don’t know why it needs this annotation - manually created classes for CoreData objects work without it). You also DO NOT have to manually install the module, because otherwise Xcode will no longer be able to find the generated class.

enter image description here

+14
source share

I had the same error. In my case, I needed to open the Swift class for the ObjectiveC runtime. To do this, add @objc(ModelClass) before declaring the Swift class as follows:

 @objc(Player) class Player: NSManagedObject { } 
+1
source share

The NSPersistentContainer name fix in AppDelegate worked for me.

 let container = NSPersistentContainer(name: "sampleDataModel") 
0
source share

All Articles