November 2016
Intermediate to advanced
480 pages
14h 42m
English
Create a new playground called Switch and set up a switch.
Listing 5.1 Your first switch
import Cocoavar str = "Hello, playground"var statusCode: Int = 404 var errorString: String switch statusCode { case 400: errorString = "Bad request" case 401: errorString = "Unauthorized" case 403: errorString = "Forbidden" case 404: errorString = "Not found" default: errorString = "None" }
The switch statement above compares an HTTP status code against four cases in order to match a String instance describing the error.
Because case 404 matches statusCode, errorString is assigned to be equal to "Not found", as you can see in the sidebar (Figure 5.1).
Try changing the value of statusCode to see the other results. When you ...
Read now
Unlock full access