7

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.

puttputt
  • 1,259
  • 2
  • 14
  • 25
  • Objc isn't the right thing to use, see http://stackoverflow.com/questions/24154117/in-swift-using-core-data-how-come-i-can-cast-to-nsmanagedobject-but-not-to-my – jrturton Aug 20 '14 at 15:45
  • 1
    Have you prefixed the class in the model editor with the module name? – jrturton Aug 20 '14 at 15:45
  • Yeah. Adding the prefix bypasses the error but causes the "swift_dynamicCastClassUnconditional" – puttputt Aug 20 '14 at 16:11
  • CoreData: warning: Unable to load class named 'rest_test.CatalogItem' for entity 'CatalogItem'. Class not found, using default NSManagedObject instead. – puttputt Aug 20 '14 at 16:22
  • I'm pretty sure that the problem is with init method. Create method instead where you inserting entity. – Shmidt Aug 20 '14 at 17:08
  • @Shmidt Yeah looks like you are right. It doesn't like me overloading the init function, looks like it can't cast with it. – puttputt Aug 20 '14 at 22:03
  • @puttputt read this http://stackoverflow.com/questions/25107032/coredata-equivalent-of-using-categories-w-example-swift?rq=1 – Shmidt Aug 21 '14 at 09:20

1 Answers1

17

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.

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118