【发布时间】:2014-10-14 01:30:26
【问题描述】:
我在尝试运行我的应用程序时遇到以下错误
致命错误:对“rcresttest.CatalogItem”类使用未实现的初始化程序“init(entity:insertIntoManagedObjectContext:)”
我可以通过将数据模型中的实体类更改为其他内容来绕过此错误,但是当我尝试向下转换时,我会得到一个 swift_dynamicCastClassUnconditional:。
这是 beta6 中的错误还是我做错了什么?
CatalogItem.swift
import CoreData
@objc(CatalogItem)
class CatalogItem : NSManagedObject {
@NSManaged var id : String
@NSManaged var slug : String
@NSManaged var catalogItemId : String
init(entity: NSEntityDescription!, context: NSManagedObjectContext!, catalogResultsDict : NSDictionary) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
id = catalogResultsDict["Id"] as String
slug = catalogResultsDict["Slug"] as String
catalogItemId = catalogResultsDict["CatalogItemId"] as String
}
}
和数据模型
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="6220.8" systemVersion="13E28" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="CatalogItem" representedClassName="CatalogItem" syncable="YES">
<attribute name="catalogItemId" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="id" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="slug" optional="YES" attributeType="String" syncable="YES"/>
</entity>
<elements>
<element name="CatalogItem" positionX="-45" positionY="0" width="128" height="90"/>
</elements>
</model>
编辑:
将数据模型类的名称更改为具有模块前缀后尝试强制转换后出现错误消息。
2014-08-20 10:49:15.335 rcresttest[63516:4194127] CoreData:警告:无法为实体“CatalogItem”加载名为“rcresttest.CatalogItem”的类。找不到类,改用默认的 NSManagedObject。
【问题讨论】:
-
Objc 不适合使用,请参阅stackoverflow.com/questions/24154117/…
-
您是否在模型编辑器中为类添加了模块名称的前缀?
-
是的。添加前缀会绕过错误,但会导致“swift_dynamicCastClassUnconditional”
-
CoreData:警告:无法为实体“CatalogItem”加载名为“rest_test.CatalogItem”的类。未找到类,改用默认的 NSManagedObject。
-
我很确定问题出在 init 方法上。在插入实体的位置创建方法。