How come I can cast to NSManagedObject but not to my entity's type?

后端 未结 8 1475
粉色の甜心
粉色の甜心 2020-12-01 13:36

I\'m using the Swift boilerplate code for Core Data in a fresh project. My .xcdatamodeld file has a single entity defined (Task) with a single attr

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 14:21

    I guess only changing the class field in the .xcdatamodel doesn't work anymore because I still got the following exception: fatal error: use of unimplemented initializer 'init(entity:insertIntoManagedObjectContext:)' for class

    So, I entered this code in my custom class:

        init(entity: NSEntityDescription!,
        insertIntoManagedObjectContext context: NSManagedObjectContext!) {
            super.init(entity: entity, insertIntoManagedObjectContext: context)
    }
    

    Suddenly, it worked! The NSManagedObject is now down-castable to my custom class. I can't really understand why this is the solution, but it works :)

提交回复
热议问题