Kodeco Forums

Magical Error Handling in Swift

In this tutorial you will learn all about error handling in Swift. You'll learn about all the new features added in Swift 2.0 and discover how to use them.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1050-magical-error-handling-in-swift

Very nice overview, thank you!

1 Like

Great tutorial on error handling! The witch theme was a really nice touch that kept the tutorial fun and interesting. I really enjoyed your writing style and use of playgrounds. Thank you.

1 Like

good to learn this article,thx

1 Like

Amazing job, quite clear and useful. Thanks!

1 Like

Avoiding Errors with Custom Handling – Starter.playground does not match the text of the article. Just wanted to let someone know.

1 Like

Thanks for the feedback - I appreciate it. I’ve made a couple of minor edits just now that should bring things back into line. Happy Swifting !

Great tutorial!! Super easy to read through and grok quickly.

1 Like

@gemmakbarlow how do you capture the customize reason string of “spellFailed” in the func handleSpellError? Thanks

1 Like

I think this modified function will do what you’re looking for:

func retrieveFailureDetails(from error: ChangoSpellError) -> String {
 var details = ""
   
    switch error {
    case .hatMissingOrNotMagical:
        details = "Did you forget your hat, or does it need its batteries charged?"
        
    case .familiarAlreadyAToad:
        details = "Why are you trying to change a Toad into a Toad?"
        
    default: break
    }
    return details
}

It stores the information into var details for each of the cases, and returns it (rather than printing it to the console).

1 Like

@gemmakbarlow Thank you for your reply. My question was how can I access value stored in the variable reason Thank you very much.

enum ChangoSpellError: Error {
case hatMissingOrNotMagical
case noFamiliar
case familiarAlreadyAToad
case spellFailed(reason: String)
case spellNotKnownToWitch
}

1 Like

No problem! Right, I understand what you’re asking more clearly now - good question.

You can extract an associated value using either let or var alongside the case statement - e.g.

case .spellFailed(let reason):
// Do something with ‘reason’ here

or

case .spellFailed(var reason):
// Do something with ‘reason’ here

Normal swift rules apply when deciding to use let or var - when in doubt, start with let.

Additionally, you could have used someReason instead of reason here - it doesn’t need to match the name given when defining the enum (though I’d recommend that to make your code nice and clear).

The official Apple documentation regarding this can be found here - The Swift Programming Language: Redirect - but it’s buried a bit, so try searching for ‘extract’ as a keyword.

Hope that helps !

1 Like

I got it now. Thank you for sharing.

1 Like

You’re very welcome ! :balloon:

Hello Gemma! Do you know how can i make non throwable async functions into synchronous?
I can do thant above:

func lastPlacemarks() throws -> [CLPlacemark]? {
  var pError: Error?
  var pPlacemarks: [CLPlacemark]?
  guard let location = lastLocation else {
    return nil
  }
  CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error)  in
    if error != nil {
      pError = error
    }
    pPlacemarks = placemarks
  })
  if (pError != nil) {
    throw pError!
  }
  return pPlacemarks
}

“lastLocation” is a filled variable in my current class, but the reverseGeocodeLocation function wasn’t called because it’s an async function, it’s running detached of current thread, this funcition returns pPlacemarks as a nil value, in all time.

Any suggestions?

This tutorial is more than six months old, so questions regarding it are no longer supported. We will update it as soon as possible. Thank you! :]