我是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
}
}
由&#34生成的类;创建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?
}
答案 0 :(得分:66)
问题不在于您的问题中的代码,而是在您作为对其他答案的评论中包含的代码段中:
var currentCourse = Course()
这不只是将currentCourse
声明为Course
类型,它还使用标准Course
方法创建init
实体的实例。明确禁止这样做:您必须使用指定的初始化程序:init(entity entity: NSEntityDescription,
insertIntoManagedObjectContext context: NSManagedObjectContext?)
。 Apple文档here中对此进行了描述。
我怀疑你没有使用上面var定义创建的实例,所以只需将其定义为Course?
类型:
var currentCourse : Course?
由于它是可选的,因此您无需设置初始值,但无论何时使用它都需要打开它。
答案 1 :(得分:11)
我有同样的问题。并且实例化这样的对象是有效的,对于你的课程,它将是这样的:
$(document).ready(function() {
var map = {
0: '', 1: 'A', 2: 'B', 3: 'D',
4: 'C', 5: 'E', 6: 'F', 7: 'G'
};
$('#checkbox1, #checkbox2, #checkbox3').change(function(event) {
var bitmask = ($('#checkbox1')[0].checked?1:0)+
($('#checkbox2')[0].checked?2:0)+
($('#checkbox3')[0].checked?4:0);
$('div').hide();
$('div#'+map[bitmask]).show();
});
$('div').hide();
})
而不是:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="checkbox1" id="checkbox1" value="" type="checkbox"> checkbox1 </label>
<label><input name="checkbox2" id="checkbox2" value="" type="checkbox"> checkbox2 </label>
<label><input name="checkbox3" id="checkbox3" value="" type="checkbox"> checkbox3 </label>
<div id="A">
Display for selected Checkbox one only
</div>
<div id="B">
Display for selected Checkbox two only
</div>
<div id="C">
Display for selected Checkbox three only
</div>
<div id="D">
Display for selected Checkboxes one and two
</div>
<div id="E">
Display for selected Checkboxes one and three
</div>
<div id="F">
Display for selected Checkboxes two and three
</div>
<div id="G">
Display for selected Checkboxes one two and three
</div>
答案 2 :(得分:6)
最简单的方法是:
在AppDelegate中(括号外):
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
在代码中:
let currentCourse = Course(context:context)
现在您已创建了您的实体。但不要忘记保存:
appDelegate.saveContext()
答案 3 :(得分:2)
I used this in Xcode 8.3.2 with Swift 3.1.
NSEntityDescription.insertNewObject(forEntityName: String(describing: type(of: Record())), into: managedObjectContext) as! Record
And got the same error message. But this data was inserted into db. So maybe this doesn't matter.
答案 4 :(得分:0)
您的currentCourse
应该是NSManagedObject类
请参阅此CoreData: error: Failed to call designated initializer on NSManagedObject class