ios - 如何处理 iOS Swift 中的 CoreData 崩溃?

标签 ios iphone swift crash

我需要处理核心数据崩溃。我的代码在 managedObjectContext.save() 上崩溃了。 但是 catch block 没有捕获到任何异常。为了避免崩溃,我怎样才能更好地编写我的 Catch block 这是我的代码。

do {
      try managedObjectContext.save()              
   } 
catch let error as NSError {
      Print.print("Error saving data store: \(error)")
     }

最佳答案

这是使用 CoreData 保存数据的示例。这可能对您有所帮助。

 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        if let entity = NSEntityDescription.entity(forEntityName: "Employees", in: context){
           let myItem = NSManagedObject(entity: entity, insertInto: context)
            myItem.setValue(nameTF.text, forKey: "names")
            myItem.setValue(15655, forKey: "mobileNo")

            do {
                try context.save()

            }catch let nserror as NSError{
                print("ERROR: Coredata error \(nserror)")
            }

        }

关于ios - 如何处理 iOS Swift 中的 CoreData 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286431/

相关文章:

objective-c - -[UIView setRegion :animated:]: unrecognized selector sent to instance

iphone - Localized.strings 有时可以工作

iphone - 如何在 XCode 中找到我的错误? (iPhone项目)

android - 手持 iPhone Android 特定 CSS 根本不起作用

ios - 无法安装 Cocoapods - 在项目目录中找不到 podfile

ios - 适用于 iPad 的 3D touch 对应

ios - 使用多个对象解析 JSON 响应

ios - 在 iOS 中使用 XMPP 将新用户注册到 Openfire 服务器

ios - 如何使用 Contacts 框架保存联系人

ios - SpriteKit 正确的 Assets 大小