Swift Switch or, the Fun Conditions Are

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"
  }
  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.

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: ()
}
  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.

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
}
  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.

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: ()
}
  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.

 

Learn more

How to Send iOS Notifications in Different Languages? Guide to Dynamic Localization

The best way to set a connection between a user, provider, and an app? System notifications. They allow users to get the latest news in no time. This solution is easy to implement in apps dedicated to one market. But it gets complicated when the messages must be displayed in many languages. In such cases, the dynamic localization of remote notifications can be a real game-changer.

Read more

Promises on the Example of PromiseKit in Objective-C

Sooner or later every programmer encounters the problem of synchronous execution of certain actions. For example: get user information from the API, parse server response, save data to the database, update the view, and many others. To make it even more, at some of these stages you still need to deal with error handling. What should you do exactly?

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2021

Winning app in
EVERYDAY LIFE

Nagroda Legalnych Bukmacherów

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2023

Winning app in MCOMMERCE DEVELOPMENT

23

client reviews

Clutch logo