Swift dynamicCast error while inserting new object to database

孤者浪人 提交于 2019-12-06 09:21:54

I solved the problem by initializing the entity object differently, if you implement the initiation method from super class(NSManagedObject) like: (in Group.swift)

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

you can create the object like this:

    var desc:NSEntityDescription = NSEntityDescription.entityForName("Group",inManagedObjectContext:appDelegate.managedObjectContext)
    var group:Group = Group(entity:desc, insertIntoManagedObjectContext: appDelegate.managedObjectContext)

most of solutions i have read was making the initiation like this:

  var group:Group = NSEntityDescription.insertNewObjectForEntityForName("Group", inManagedObjectContext:appDelegate.managedObjectContext) as Group

so this was the problem and object could not be casted to custom managed object class with this way. Thanks to everyone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!