I am developing an application that uses Xcode database for the first time. By following an online tutorial I got the idea how to do it step by step, but when I test the app I get runtime exception when it reaches the line which saves the data into the database.
This is the code I wrote so far, the idea is when a star button pressed the user information will be saved
@IBAction func favPressed(sender: AnyObject) {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
// here is where the app crushes:
let newFav = NSEntityDescription.insertNewObjectForEntityForName("FavoriteUser", inManagedObjectContext: context) as! NSManagedObject
newFav.setValue("" + userName, forKey: "uName")
newFav.setValue("" + userEmail, forKey: "uEmail")
newFav.setValue("" + userPhone, forKey: "uPhone")
do{
try context.save()
}
catch{
print(error)
}
print("user saved to favs")
}
I have no idea why this is happening, and I don't know if this will be helpful but note that I used the Core Data into an existing big project. I have added the CoreData.framework and the required functions in the appDelegate.swift