iOS8 CoreData“使用未实现的初始化器” [英] iOS8 CoreData "use of unimplemented initializer"
问题描述
尝试运行我的应用程序时出现以下错误
致命错误:对'rcresttest.CatalogItem类使用未实现的初始化器'init(entity:insertIntoManagedObjectContext :)' '
我可以绕过这个错误,通过改变数据模型中的Entity的类别,但是我会得到一个swift_dynamicCastClassUnconditional:当试图downcast。 >
这是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.0encoding =UTF-8standalone =yes?>
< model userDefinedModelVersionIdentifier =type =com.apple.IDECoreDataModeler.DataModeldocumentVersion =1.0lastSavedToolsVersion =6220.8systemVersion =13E28minimumToolsVersion =Xcode 4.3macOSVersion =Automatic 自动>
< entity name =CatalogItem \"resentClassName =CatalogItemsyncable =YES>
< attribute name =catalogItemIdoptional =YESattributeType =Stringsyncable =YES/>
< attribute name =idoptional =YESattributeType =Stringsyncable =YES/>
< attribute name =slugoptional =YESattributeType =Stringsyncable =YES/>
< / entity>
< elements>
< element name =CatalogItempositionX = - 45positionY =0width =128height =90/>
< / elements>
< / model>
编辑:
2014-08-20 10:49:15.335 rcresttest [63516:4194127] CoreData :warning:无法加载实体'CatalogItem'命名为'rcresttest.CatalogItem'的类。类未找到,使用默认NSManagedObject。
这是指定的初始化程序的问题。只需在 init
前添加方便并调用
init(entity:insertIntoManagedObjectContext:)
I get the following error trying to run my application
fatal error: use of unimplemented initializer 'init(entity:insertIntoManagedObjectContext:)' for class 'rcresttest.CatalogItem'
I can bypass this error by changing the Entity's class in the data model to something else, but then I will get a swift_dynamicCastClassUnconditional: when trying to downcast.
Is this a bug in beta6 or am I doing something wrong?
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
}
}
and the data model
<?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>
Edit:
After changing the name of the datamodel class to have the module prefix The error message appears after trying to cast.
2014-08-20 10:49:15.335 rcresttest[63516:4194127] CoreData: warning: Unable to load class named 'rcresttest.CatalogItem' for entity 'CatalogItem'. Class not found, using default NSManagedObject instead.
This is a problem with the designated initializer. Just add convenience
in front of your init
and call init(entity:insertIntoManagedObjectContext:)
on self
instead super
.
这篇关于iOS8 CoreData“使用未实现的初始化器”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!