How can I print out Core Data save error in Swift?
As far as I have this two lines:
var error: AutoreleasingUnsafePointer<NSError?> = nil
let success = managedObjectContext.save(error)
func saveContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch let nserror as NSError {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
print("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
}
}
You have to pass a pointer to an optional NSError
object. As an example, this is the template code from Xcode if you create an iOS app and select the "Use Core Data" checkbox:
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
// Replace this implementation with code to handle the error appropriately.
// ...
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.