【发布时间】: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?
}
【问题讨论】: