初めて Xcode データベースを使用するアプリケーションを開発しています。オンラインチュートリアルに従うことで、それを段階的に行う方法がわかりましたが、アプリをテストすると、データをデータベースに保存する行に到達するとランタイム例外が発生します。
これは私がこれまでに書いたコードです。アイデアは、星のボタンが押されたときにユーザー情報が保存されるというものです
@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")
}
なぜこれが起こっているのかわかりません。また、これが役立つかどうかもわかりませんが、Core Data を既存の大きなプロジェクトに使用したことに注意してください。CoreData.framework と必要な関数を appDelegate.swift に追加しました