Core data 解决';未能调用NSManagedObject类上的指定初始值设定项';

Core data 解决';未能调用NSManagedObject类上的指定初始值设定项';,core-data,swift2,Core Data,Swift2,我是Swift的新手,我正在努力学习如何使用核心数据。但是我犯了这个错误,我不确定我做错了什么。我在网上搜索过,尝试过一些东西,但都没有找到正确的答案 Failed to call designated initializer on NSManagedObject class 'FirstCoreData.Course' 执行此行时: ncvc.currentCourse = newCourse 在该功能中: class TableViewController: UITableViewCon

我是Swift的新手,我正在努力学习如何使用核心数据。但是我犯了这个错误,我不确定我做错了什么。我在网上搜索过,尝试过一些东西,但都没有找到正确的答案

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

}
以及:


您的
currentCourse
应该是NSManagedObject类


请参考此

问题不在于您问题中的代码,而在于您作为其他答案的注释包含的代码片段:

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

我怀疑您从未使用过由上述var定义创建的实例,因此只需将其定义为
课程?

var currentCourse : Course?

因为它是可选的,所以不需要设置初始值,尽管每次使用时都需要打开该值。

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

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

var currentCourse = Course()

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

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

并收到相同的错误消息。但是这些数据被插入到数据库中。所以这也许没关系。

最简单的方法是:

  • 在ApplicationLegate中定义上下文的引用
  • 通过传递上下文实例化变量
在AppDelegate中(括号外):

在守则中:

let currentCourse = Course(context:context)
现在您已经创建了实体。但别忘了使用以下工具进行保存:

appDelegate.saveContext()

如果我将currentCourse声明为NSManagedObject,我将失去对标题、作者和releaseDate成员的访问权限。因此,我不知道如何访问实体属性。我认为currentCourse也是课程类,因此您不会丢失实体。抱歉,我不明白。目前我有var currentCourse=Course()。如果我将其更改为var currentCourse=NSManagedObject(),那么所有引用课程成员的内容(例如currentCourse.title)都会说“title不是NSManagedObject的成员”。由于课程在NSManagedObject类中,您应该开始使用它,
initWithEntity:insertIntoManagedObjectContext:
我在Objective-C中找到了很多这样的例子,但我很难找到如何在Swift中实现这一点。自动完成弹出窗口在initWithEntity中找不到任何内容,我甚至在Swift的苹果文档中很难找到它。你能告诉我如何在Swift中使用它吗?正确的建议!太好了,谢谢,你节省了我的时间。这一行帮助我解决了我的问题:让currentCourse=Course(context:context)。我返回它时没有任何参数,但在添加(context:myManagedObjectContext)后,它解决了我的问题。
let currentCourse = Course(context:context)
appDelegate.saveContext()