【问题标题】:Resolving 'Failed to call designated initializer on NSManagedObject class'解决“无法在 NSManagedObject 类上调用指定的初始化程序”
【发布时间】:2016-01-22 22:13:03
【问题描述】:

我是 Swift 新手,我正在尝试学习如何使用 Core Data。但是我收到了这个错误,我不确定我做错了什么。我在网上搜索并尝试了一些方法,但我无法正确。

Failed to call designated initializer on NSManagedObject class 'FirstCoreData.Course'

当这行执行时:

ncvc.currentCourse = newCourse

在这个函数中:

class TableViewController: UITableViewController, AddCourseViewControllerDelegate {

var managedObjectContext = NSManagedObjectContext.init(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "addCourse" {
        let ncvc = segue.destinationViewController as! NewCourseViewController
        ncvc.delegate = self

        let newCourse = NSEntityDescription.insertNewObjectForEntityForName("Course", inManagedObjectContext: self.managedObjectContext) as! Course
        ncvc.currentCourse = newCourse

    }
}

由“创建 NSManagedObject 子类...”为课程实体生成的类:

import Foundation
import CoreData

class Course: NSManagedObject {

// Insert code here to add functionality to your managed object subclass

}

还有:

import Foundation
import CoreData

extension Course {

    @NSManaged var title: String?
    @NSManaged var author: String?
    @NSManaged var releaseDate: NSDate?

}

【问题讨论】:

    标签: core-data swift2


    【解决方案1】:

    问题不在于您问题中的代码,而在于您作为 cmets 包含在另一个答案中的 sn-p:

    var currentCourse = Course()
    

    这不仅将currentCourse 声明为Course 类型,还使用标准init 方法创建Course 实体的实例。这是明确不允许的:您必须使用指定的初始化程序:init(entity entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)。这在 Apple 文档here 中有描述。

    我怀疑你从来没有使用过上述 var 定义创建的实例,所以只需将其定义为 Course? 类型:

    var currentCourse : Course?
    

    由于它是可选的,因此您不需要设置初始值,但您需要在使用时解开该值。

    【讨论】:

    • 正确的建议!太好了。
    • 谢谢,您节省了我的时间。并且需要注意。
    【解决方案2】:

    最简单的方法是这样的:

    • 在 applicationDelegate 中定义上下文的引用
    • 通过传递上下文来实例化变量

    在 AppDelegate 中(括号外):

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    

    在代码中:

    let currentCourse = Course(context:context)
    

    现在您已经创建了实体。但不要忘记保存:

    appDelegate.saveContext()
    

    【讨论】:

    • 这一行帮助我解决了我的问题:让 currentCourse = Course(context:context).我在没有任何参数的情况下返回它,但在添加 (context: myManagedObjectContext) 后它解决了我的问题。
    【解决方案3】:

    我遇到了同样的问题。并且像这样实例化对象是有效的,对于你的课程来说,它会是这样的:

    var currentCourse = Course.init(entity: NSEntityDescription.entityForName("Course", inManagedObjectContext:mox)!, insertIntoManagedObjectContext: mox)
    

    代替:

    var currentCourse = Course()
    

    【讨论】:

      【解决方案4】:

      我在 Xcode 8.3.2 和 Swift 3.1 中使用了它。

      NSEntityDescription.insertNewObject(forEntityName: String(describing: type(of: Record())), into: managedObjectContext) as! Record
      

      并得到相同的错误消息。但是这个数据被插入到db中。所以也许这无关紧要。

      【讨论】:

        【解决方案5】:

        你的currentCourse 应该是 NSManagedObject 类

        请参考CoreData: error: Failed to call designated initializer on NSManagedObject class

        【讨论】:

        • 如果我将 currentCourse 声明为 NSManagedObject 我将失去对 title、author 和 releaseDate 成员的访问权限。那么我不知道如何访问实体属性。
        • 我认为 currentCourse 也是 Course 类,所以你不会丢失实体。
        • 对不起,我不明白。目前我有 var currentCourse = Course().如果我将其更改为 var currentCourse = NSManagedObject() 那么所有涉及课程成员(例如 currentCourse.title)的内容都会说“标题不是 NSManagedObject 的成员”。
        • 既然课程在 NSManagedObject 类,你应该开始使用这个,initWithEntity:insertIntoManagedObjectContext:
        • 我在 Objective-C 中找到了很多这样的例子,但我正在努力寻找如何在 Swift 中做到这一点。自动完成弹出窗口没有为 initWithEntity 找到任何内容,我什至很难在 Apple 文档中的 Swift 中找到它。你能告诉我如何在 Swift 中使用它吗?
        猜你喜欢
        • 2016-11-18
        • 2014-10-19
        • 2013-01-18
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多