Swift Switch or, the Fun Conditions Are
I would like you to get acquainted with a switch conditional statement in Swift, using the rubber duck method. I assume that you already know the concept of the switch statement, therefore, I will show you what is new in Swift. Let’s play with conditions!
The most used example
Here I will show you a piece of code, which I will describe in the next section.
let string = "Hello, playground" //1
let value = false
var endString: String //2
switch string {
case "Hello, playground" where value: //3
endString = "case one with value"
case "a", "b", "c": //4
endString = "case with 3 values"
case "Goodbye playgrand": //5
endString = "Goodbye"
case _ where !value: //6
endString = "Yo!"
default: //7
endString = "default"
}
- I create a string constant.
- The variable string, which we will overwrite in the switch.
- The value of the string is correct, but I added “where”, which also checks the value specified further.
- You can give several values after a comma, and if either is correct, the switch enters into given case.
- The value given is not correct, the case will also be omitted.
- Instead of specifying value you can give a lower dash, which means that regardless of this value, we should enter the case.
- The word “default” means that the case will be entered only if all other values do not match.
Optionals
let text = "Text" as AnyObject? //1
switch text {
case let text? where text is Int: //2
print(text as! Int)
case let text? where text is String: //3
print(text as! String)
default: ()
}
- Another variable with a string value, but declared as an optional subject of Any.
- Check if the text is nil and then, with “where”, check whether the object is an int.
- Check if the text is nil and then, with “where”, check whether the object is a string.
Ints
let a = 1
let b = 2
var c = 0
switch (a, b) { //1
case (1, 2), (2, 3): //2
c = 12
fallthrough //3
case (2,_): //4
c += 1
case (let aValue, 2): //5
print("a = \(aValue)")
case (let eitherValue, 2), (2, let eitherValue): //6
print("either value: \(eitherValue)")
case (0...3, 0...100): //7
c += 5
fallthrough
default:
c = 17
}
- In case of such code, the switch will check both constants.
- This way, you can handle several cases at once, using only one condition.
- The word “fallthrough” means a transition to the next condition, so if (a, b) == (1, 2) it will proceed to the next true condition.
- In this case, the switch will check only the first value and completely skip the second.
- The switch will monitor only the second value, and with the first it will create a constant, in this case called “aValue”.
- Here, I wanted to show that the values can be formed from a variety of parameters, which in the first case is the first value and in the second the second value.
- This case demonstrates the use of numerical ranges, here switch checks whether the first value will be in the range of 0 to 3 and the other from 0 to 100.
Enums
enum Cases { //1
case noParameters
case oneParameter(text: String)
case anotherOneParameter(firstText: String)
case twoParameters(text: String, number: Int)
}
let thisCase: Cases = .twoParameters(text: "Text", number: 10) //2
switch thisCase {
case .oneParameter(_): //4
break
case .anotherOneParameter(let text): //5
print("Text: \(text)")
case let .twoParameters(text, number): //6
print("Text: \(text)")
print("Number: \(number)")
default: ()
}
- Declaration of enum with 3 values, the first with no parameters, the second and third with one parameter and the fourth with two parameters.
- Creating a constant with two parameters.
- Check if the constant is equal to .noParameters.
- Check if the constant is equal to .oneParameter and skip values.
- Check if the constant is equal to .anotherOneParameter, giving access to parameters.
- The same as in step 5, only the word “let” is now before the name, so you don’t have to write it in front of each parameter.
The code used in this article is on our GitHub.