核心数据模型似乎没有反映在应用程序中 [英] Core Data Model doesn't appear to be reflected in app

查看:169
本文介绍了核心数据模型似乎没有反映在应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode 8 beta 1中遇到核心数据有困难。旧应用程序将编译并运行正常,但所有新应用程序编译并运行正常,直到尝试插入新的NSManagedObject。

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

最初我认为它与错误地删除旧的xcdatamodel并重新构建另一个有关,但在制作一个全新的应用程序并制作一个简单的实体A之后,我无法创建一个类的对象A在运行时。

Initially I thought it had something to do with incorrectly deleting an old xcdatamodel and remaking another, but after making a brand new app and making a simple Entity "A", I am not able to create an object of class A at runtime.

我尝试使用让a = A(context:myMOC)返回错误:

I have tried using let a = A(context: myMOC) which returns the error:


类'MyApp.A'的NSManagedObject必须具有有效的NSEntityDescription。

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

尝试旧的让a = NSEntityDescription.insertNewObject(forEntityName:A,into:context)为! A 返回错误:


无法转换类型'NSManagedObject_A_'的值
(0x7fd114626f80)到'MyApp.A'(0x10d2fdf28)。

Could not cast value of type 'NSManagedObject_A_' (0x7fd114626f80) to 'MyApp.A' (0x10d2fdf28).

我已经检查了我的xcdatamodel十几次,以确保我拼写了所有内容,并且创建了一个新项目进行测试,以确保我在设置CD时没有出现任何错误。想法?

I have checked my xcdatamodel a dozen times to ensure I have spelt everything correctly, and have created a new project to test to make sure I haven't made any errors in setting up CD. Thoughts?

更新: xcdatamodel包内容如下:

UPDATE: The xcdatamodel package contents are this:

<?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>

更新2:打印managedObjectModel的实体,表明正确的模型是被装载。我仍然无法在任何新项目中初始化NSManagedObject。

UPDATE 2: Printing the managedObjectModel's entities, shows that the correct model is being loaded. Still I can't get an NSManagedObject to initialize in any new project.

推荐答案

我终于明白了:

在Xcode 7中,您必须输入类名,并在Swift中将模块设置为当前项目模块。我认为在Xcode 8中不再需要,因为它会自动生成源文件。你必须手动将类名设置为生成的类的名称,以使Xcode生成 @objc(ClassName)(我不知道它为什么需要它注释 - 为CoreData实体手动创建的类可以在没有它的情况下工作。此外,您不必手动设置模块,否则Xcode将无法再找到生成的类。

In Xcode 7 you would have to enter the class name and, in Swift, set the module to "Current Project Module". I assumed that wouldn't be necessary anymore in Xcode 8, since it automatically generates the source files. You do have to manually set the class name to the name of the generated class though, to make Xcode generate the @objc(ClassName) (i have no idea why it needs that annotation - manually created classes for CoreData entities work without it for me). Also you have to NOT manually set the module, because otherwise Xcode can't find the generated class anymore.

这篇关于核心数据模型似乎没有反映在应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆