Things don't always go as you would expect, so you need to keep that in mind as you write apps. You need to think of ways that your app might fail, and what to do if it does.
Let's say you have an app that needs to access a web page. However, if the server where that web page is located is down, it is up to you to write the code to handle the error—for example, trying an alternative web server or informing the user that the server is down.
First, you need an object that conforms to Swift's Error protocol:
- Type in the following code into your playground:
// Error handling
// Create an enum that adopts the error protocol
enum WebpageError: Error {
case success
// associate server response value of type Int with failure
case failure(Int)
}
The code you entered declares an enumeration, WebpageError, that has adopted the Error...