How To Use Swift Switch Case Example

Switch case is a powerful condition check expression which exist in many programming languages. But in swift, switch case expression add some useful features in usage, it provide more powerful usage than other programming language. This article will show you some swift switch case usage examples to show the differences.

1. General Switch Case Usage – Match Variable Value Example.

You should be familiar with the general usage of switch case expression, it accept a variable, and execute code when the variable’s value match a case value. If no case value is matched, then execute the code in default case. Below is the example code.

You had better create a Xcode playground project to run below example source code, you can see the example code’s result immediately when use Xcode playground. You can read article How To Use Xcode Playground Project Example to learn more.

// Define a constant variable with default value.
let os = "Linux"

switch os {
    
case "iOS":
    print("\(os) is most popular mobile os.")

case "Windows":
    print("\(os) is most popular desktop os.")

// If no case value matched, then run default case code.
default:
    print("Mac os is the best desktop os.")
}

Below is above code execution output.

Mac os is the best desktop os.

2. Match A Range Of Value In Swift Switch Case.

The case value in swift can be a range of value such as 1...10, so when the variable’s value exist in the case value range then the case code will be executed. Below is the example.

let age = 18

switch age {
case 0...10:
    print("You are a child.")

case 11...18:
    print("You are young.")

case 19...200:
    print("You grow up")

default:
    print("Your age is not valid.")

}

Run above code will get below output.

You are young.

3. Match Multiple Value Use Tuple In Swift Switch Case.

Swift switch case can also compare multiple values through tuple. In below example, we define a tuple variable that has two elements, and the case condition is also tuple which contains one range value and one string value.

// salary is a tuple variable that has two value.
let salary = (5000, "low")

// when each of the salary tuple's value match the case tuple values then the code will be executed.
switch salary {
case (0...5000, "low"):
    print("Salary is low.")
case (5000...9000, "middle"):
    print("Salary is middle.")
case (9000...20000, "high"):
    print("Salary is high.")
default:
    print("Salary out of bounds.")
}

Below is above code output.

Salary is low.

4. Binding Tuple Value To Variable In Swift Switch Case.

You can also bind tuple variable’s elements to local variable after case expression like below.

// tuple variable car has 2 elements.
let car = ("5000 $", "ford")

// in below case statement, we bind the tuple variable's first element to a variable price, and bind the second element to a variable brand.
switch car {

case (let price, "audi"):
    print("Audi car's price is \(price).")

case (let price, "bmw"):
    print("BMW car's price is \(price).")

// only this case execute, because above case do not match the car element's value. So this is similar as the default case. And we bind the tuple elements to two variable.
case (let price, let brand):
    print("\(brand) car's price is \(price).")

}

Run above code will get below result.

ford car's price is 5000 $.

5. Use Where Statement In Swift Switch Case Expression.

Besides binding tuple variable’s elements to case variables, you can also use where in the case expression to add more condition. You can use the case variable to compare in the where clause.

let car = ("5000 $", "audi")

switch car {

case (let price, let brand) where brand == "audi":
    print("Audi car's price is \(price).")

case (let price, let brand) where brand == "bmw":
    print("BMW car's price is \(price).")

case (let price, let brand):
    print("\(brand) car's price is \(price).")

}

Below is above source code output.

Audi car's price is 5000 $.
READ :   iOS UITextField And UITextView Swift Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.