[英]Failed loading designated initializer core data
I have been struggling with creating a class to manage Core Data. 我一直在努力创建一个类来管理Core Data。 I cannot seem to get passed the "Failed to call designated initializer on NSManaged Object" 我似乎无法通过“无法在NSManaged Object上调用指定的初始化程序”
How can I fix this? 我怎样才能解决这个问题?
This is my created class to manage the data : 这是我创建的用于管理数据的类:
import Foundation
import CoreData
import UIKit
class Hours: NSManagedObject {
@NSManaged var date: NSDate
@NSManaged var startTime: NSDate
@NSManaged var endTime: NSDate
@NSManaged var totalTime: Double
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?){
let entity = NSEntityDescription.entityForName("Hours", inManagedObjectContext: context!)
super.init(entity: entity!, insertIntoManagedObjectContext: context)
}
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext {
get{
return appDel.managedObjectContext
}
}
class func insertTimes(date: NSDate, startTime:NSDate, endTime:NSDate,totalTime:Double) -> Hours{
let hour:Hours = Hours()
let newDate:Hours = NSEntityDescription.insertNewObjectForEntityForName("Hours", inManagedObjectContext: hour.context) as! Hours
print("date = \(date), start = \(startTime), end = \(endTime), total = \(totalTime)")
newDate.date = date
newDate.startTime = startTime
newDate.endTime = endTime
newDate.totalTime = totalTime
/*
newDate.setValue(date, forKey: "date")
newDate.setValue(startTime, forKey: "startTime")
newDate.setValue(endTime, forKey:"endTime")
newDate.setValue(totalTime, forKey: "totalTime")
*/
do{
try hour.context.save()
print("Succesfully saved")
}catch{
print("Unresolved save error")
}
return newDate
}
func returnDate(){
//Does not work YET
let request = NSFetchRequest(entityName: "Hours")
//request.returnsObjectsAsFaults = false
do{
let results = try context.executeFetchRequest(request)
print(results)
}catch{
print("Unresolved fetch error")
}
}
}
I try to call the method insertTimes
with the following line of code, after I press a button. 在按下按钮后,我尝试使用以下代码行调用方法insertTimes
。
@IBAction func saveData(sender: AnyObject) {
let inserted:Hours = Hours.insertTimes(date, startTime: startEndDate.startTime, endTime: startEndDate.endTime, totalTime: timeInterval)
}
Please note that all my variables are of the right types. 请注意,我所有的变量都是正确的类型。
Source can be found on: https://github.com/bbriann123/PayDay/blob/master/Hours.swift 可以在以下位置找到源: https : //github.com/bbriann123/PayDay/blob/master/Hours.swift
Look a these: 看这些:
In AppDelegate
you use "Loon Calculator" instead of "DayDataModel": https://github.com/bbriann123/PayDay/blob/master/Loon%20Calculator/AppDelegate.swift#L55 (the data model file name is clearly "DayDataModel": https://github.com/bbriann123/PayDay/tree/master/Loon%20Calculator/DayDataModel.xcdatamodeld/DayDataModel.xcdatamodel ) 在AppDelegate
,使用“ Loon Calculator”代替“ DayDataModel”: https : //github.com/bbriann123/PayDay/blob/master/Loon%20Calculator/AppDelegate.swift#L55 (数据模型文件名显然是“ DayDataModel” : https : //github.com/bbriann123/PayDay/tree/master/Loon%20Calculator/DayDataModel.xcdatamodeld/DayDataModel.xcdatamodel )
In the data model you don't specify "Hours" as the custom class of the "Hours" entity (look in the Core Data inspector) 在数据模型中,您未将“小时”指定为“小时”实体的自定义类(请在“核心数据”检查器中查看)
Also, it's weird how you reach to the NSManagedObjectContext
: https://github.com/bbriann123/PayDay/blob/master/Hours.swift#L25-L30 此外,您到达NSManagedObjectContext
的方式也很奇怪: https : //github.com/bbriann123/PayDay/blob/master/Hours.swift#L25-L30
Maybe you need a better Core Data book (or whatever you're using to learn)? 也许您需要一本更好的Core Data书籍(或您正在使用的任何学习方法)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.