Note that the value we were switching on result now becomes an initializer for the case statement. The last example written as a guard statement:. A switch statement is fine when I have something to do for all or most of the cases. The Data. It either succeeds, returning void , or fails with an error. Switching over the result in this case is annoying:.
I still find using the result value as the initializer for the case-let confusing but I prefer it over the more verbose switch version. What do you think? Do you have a favorite use for case let? Since the value matches with case "Dwight", 38 , the statement print "Dwight is 38 years old" is executed. Course Index Explore Programiz. Swift if Swift Arrays. Dictionaries in Swift. Swift Optionals. Learning Paths. Courses Become a Python Master. Become a C Master. Popular Examples Add two numbers.
Check prime number. Find the factorial of a number. Print the Fibonacci sequence. Check leap year. Swift Expressions, Statements and Code blocks. Swift if, if Swift switch Statement In this article, you will learn to use switch control statements to control the flow of your program's execution. If the result of the expression is equal to value1 , statements of the case value1: are executed. If the result of the expression is equal to value2 , statements of the case value2: are executed.
If there is no match, statements of the default case are executed. Switch Statement with fallthrough If we use the fallthrough keyword inside the case statement, the control proceeds to the next case even if the case value does not match with the switch expression. Previous Tutorial:. Next Tutorial:. Share on:. Did you find this article helpful?
Cases are evaluated in the order that they are written, and once a match is found, only the handling code for that case is handled. You can opt out of this behaviour by using the fallthrough keyword. Subsequent cases will then also be evaluated for a match. Another useful keyword is break which will immediately exit the evaluation of the switch statement. A common use case for this is to explicitly avoid handling a certain case. In this case, break is used to exit the switch without evaluating any other cases.
This means default cases are only enforced where they are truly necessary. These can easily be unpacked for use in handling code by assigning them as part of the case. Any switch case can have its matching extended by an expression returning a boolean using a where statement.
This can match against a variable or constant assigned inside the case, or anywhere in the scope of the switch statement. Cases matching with a where statement should be placed above a more general case of the same match, otherwise the general case will be matched first. You can match against any basic type in the Swift language, such as Int, String or Array. Because these types can have almost any value, the compiler will always prompt you to provide a default case.
In Swift, basic types such as Int and String are backed by structs. This allows the use of a Swift language feature called extensions, which allow you to add custom methods or static properties onto previously defined types.
This allows us to define static properties on something like an Int to make switch cases more readable. The Swift compiler can determine the type of the value we are switching on, so it can use type inference to resolve the type to call the static property on.
A tuple in Swift is a compound type that contains any number values of other types. The contained values can be named or unnamed. You can match each value of the tuple individually, or any combination of the values, allowing some very flexible matching:. This can be done either using the is operator if you simply want to check type, or using the as operator if you want to retain the cast for future use:.
That is, if there's the possibility of your variable having a value you don't check for, Xcode will refuse to build your app. In situations where the values are effectively open ended, like our liveAlbums integer, you need to include a default case to catch these potential values. Yes, even if you "know" your data can only fall within a certain range, Swift wants to be absolutely sure.
Swift can apply some evaluation to your case statements in order to match against variables. For example, if you wanted to check for a range of possible values, you could use the closed range operator like this:. If you're used to writing break in your case blocks, you should know this isn't needed in Swift. Instead, you use the fallthrough keyword to make one case fall into the next — it's effectively the opposite. Of course, if you have no idea what any of this means, that's even better: don't worry about it!
Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.
Cases can match many different patterns, including interval matches, tuples, and casts to a specific type. Matched values in a switch case can be bound to. Swift switch Statement · If the result of the expression is equal to value1, statements of the case value1: are executed. · If the result of the expression is. The switch statement in Swift lets you inspect a value and match it with a number of cases. It's particularly.