Writing Safer Code with Availability and Error Handling

When I first started writing applications with Objective-C, one of the most noticeable deficiencies was the lack of exception handling. Most modern programming languages, such as Java and C#, use try...catch blocks, or something similar, for exception handling. While Objective-C did have the try...catch block, it wasn't used within the Cocoa framework itself, and it never really felt like a true part of the language. I have significant experience in C, so I was able to understand how Apple's frameworks received and responded to errors. To be honest, I sometimes preferred this method, even though I had grown accustomed to exception handling with Java and C#. When Swift was first introduced, I was hoping that Apple would put true error handling into the language so we would have the option of using it; however, it was not in the initial release of Swift. When Swift 2 was released, Apple added error handling to Swift. While this error handling may look similar to exception handling in Java and C#, there are some very significant differences.

We will cover the following topics in this chapter:

  • How to use the do-catch block in Swift?
  • How to represent errors?
  • How to use the defer statement?
  • How to use the availability attribute?
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset