I'm using 'CoreData', I made a 'NSManagedObject' type class of my Entity named 'Project', but when I open the 'ViewController' where I initialize the class as a property, I get this error
2015-12-09 14:15:54.961 MyProject[790:17582] CoreData: error: Failed to call designated initializer on NSManagedObject class 'MyProject.Project'
In my Project NSManagedObject class I have a method to add a child NSManagedObject into a NSOrderdSet:
class Project: NSManagedObject {
func requestAddNewWeek (value : Content) {
guard let mutableCopyOfSet = projectContent?.mutableCopy() as? NSMutableOrderedSet else {
return
}
mutableCopyOfSet.addObject(value)
projectContent = mutableCopyOfSet.copy() as? NSOrderedSet
}
}
But because The 'Project' does not initialize correctly, every time I call the method in my viewcontroller an nil is Unwrapped and a error happens.
@IBAction func saveButtonPressed(sender: AnyObject) {
//
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext as NSManagedObjectContext
let entity = NSEntityDescription.entityForName("WeekContent", inManagedObjectContext:managedContext)
let newWeek = Content(entity: entity!, insertIntoManagedObjectContext: managedContext)
//Here I call project, but it unwraps a nil
project.requestAddNewWeek(newWeek)
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
print(entity)
}
Here is how I defined 'Project' in my ViewController:
var project : Project = Project()
How do I fix this.