1

HI i am a little confused about how to transfer if_else error handling to do try catch successfully.

Here is my code.

let error : NSError?
if(managedObjectContext!.save()) {
    NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)    
    if error != nil {
       print(error?.localizedDescription)
    }
}
else {
    print("abort")
    abort()
}

and now i converted to swift 2.0 like this

do {
   try managedObjectContext!.save()
}
catch {
     NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)
     print((error as NSError).localizedDescription)
}

I am confused about where to print abort and do the abort() function

Any idea~? Thanks a lot

7
  • In your original code, where does error come from? It is not coming from save().
    – Sulthan
    Commented Oct 2, 2015 at 15:32
  • @Sulthan thanks for ur remind. However after changing my code, it occurs that : Variable 'error' used before being initialized
    – J.Chang
    Commented Oct 2, 2015 at 15:42
  • @Sulthan - No, the error is thrown by the save(). When you have catch without a let, it automatically uses error as a reference to the thrown error.
    – Rob
    Commented Oct 2, 2015 at 15:44
  • @Rob I was speaking about the error in the first example.
    – Sulthan
    Commented Oct 2, 2015 at 15:46
  • Ok. That first example was obviously a typo because it isn't valid Swift 1.2 code. So I assumed you must have meant the second code snippet. But you're right that that first code snippet is missing the error reference in the save() call.
    – Rob
    Commented Oct 2, 2015 at 15:50

2 Answers 2

1

Rewriting your code to work the same as your original code

do {
   try managedObjectContext!.save()

   //this happens when save did pass
   NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)    

   //this error variable has nothing to do with save in your original code
   if error != nil {
       print(error?.localizedDescription)
   }
}
catch {
   //this happens when save() doesn't pass
   abort()
}

what you probably want to write is the following:

do {
   try managedObjectContext!.save()

   //this happens when save did pass
   NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)    
}
catch let saveError as NSError {
   //this happens when save() doesn't pass
   print(saveError.localizedDescription)
   abort()
}
1
  • I now understand your first code snippet. You assumed that he had some other error variable floating around. His revised question makes it clear that this is not the case, but I now get why you wrote what you wrote.
    – Rob
    Commented Oct 2, 2015 at 16:03
1

Everything within do {} is good, everything within catch {} is bad

do {
   try managedObjectContext!.save()
   NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)
}
catch let error as NSError {
     print(error.localizedDescription)
     abort()
}

use either the error handling or the abort() statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.