"Powerful new ways to control the flow of your code."
In Swift, switch statements are introduced as "A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first pattern that matches successfully." Anyone used to using a C based language, such as Objective-C or C++, will know that the only form of 'matching' is with integer values.
This essentially reduces a 'switch' statement to shorthand for a bloated if....else statement. “Matching patterns" does not just mean other types such as doubles, strings and non integer enumerations. It means values within tuples, matching against ranges and even matching against additional conditions.
All of this functionality gives swift developers powerful new ways to control the flow of their code. One obvious use is to neaten complex table and collection views but that is not the only use. This presentation will attempt to introduce this powerful language feature and detail some interesting uses which have arisen in the early days of the language.
YOU MAY ALSO LIKE: