gpt4 book ai didi

ios - 核心数据模型似乎没有反射(reflect)在应用程序中

转载 作者:IT王子 更新时间:2023-10-29 05:48:09 26 4
gpt4 key购买 nike

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

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

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

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

尝试使用旧的 let a = NSEntityDescription.insertNewObject(forEntityName: "A", into: context) 作为! A 返回错误:

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

我检查了我的 xcdatamodel 十几次以确保我拼写正确,并创建了一个新项目来测试以确保我在设置 CD 时没有犯任何错误。想法?

更新: 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>

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

最佳答案

我终于明白了:

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

enter image description here

关于ios - 核心数据模型似乎没有反射(reflect)在应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com