22 February

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.

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"

  • 1 / I create a string constant
  • 2 / The variable string, which we will overwrite in the switch
  • 3 / The value of the string is correct, but I added "where", which also checks the value specified further
  • 4 / You can give several values after a comma, and if either is correct, the switch enters into given case
  • 5 / The value given is not correct, the case will also be omitted
  • 6 / Instead of specifying value you can give a lower dash, which means that regardless of this value, we should enter the case
  • 7 / The word "default" means that the case will be entered only if all other values ​​do not match.
  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: ()

  • 1 / Another variable with a string value, but declared as an optional subject of Any
  • 2 / Check if the text is nil and then, with "where", check whether the object is an int
  • 3 / Check if the text is nil and then, with "where", check whether the object is a string
  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
      c = 17

  • 1 / In case of such code, the switch will check both constants
  • 2 / This way, you can handle several cases at once, using only one condition
  • 3 / The word "fallthrough" means a transition to the next condition, so if (a, b) == (1, 2) it will proceed to the next true condition
  • 4 / In this case, the switch will check only the first value and completely skip the second
  • 5 / The switch will monitor only the second value, and with the first it will create a constant, in this case called "aValue"
  • 6 / 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
  • 7 / 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
  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
  case .anotherOneParameter(let text): //5
      print("Text: \(text)")
  case let .twoParameters(text, number): //6
      print("Text: \(text)")
      print("Number: \(number)")
  default: ()

  • 1 / Declaration of enum with 3 values, the first with no parameters, the second and third with one parameter and the fourth with two parameters
  • 2 / Creating a constant with two parameters
  • 3 / Check if the constant is equal to .noParameters
  • 4 / Check if the constant is equal to .oneParameter and skip values
  • 5 / Check if the constant is equal to .anotherOneParameter, giving access to parameters
  • 6 / 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.