Day 2 - Swift Booleans and String Interpolation
Table of Contents
In today’s lesson, we will look at Booleans, a data type that stores the result of logical conditions, and Interpolation, an important feature that we can use in String expressions.
How to Store Logical State: Boolean #
The Boolean type is quite simple. It can only store true and false.
Boolean data can be created as follows. It has no values other than true
and false
.
let goodDogs = true
let gameOver = false
In fact, we saw this data type in the subjects we studied on day 1.
let filename = "paris.jpg"
print(filename.hasSuffix(".jpg"))
//OUTPUT : true
let number = 120
print(number.isMultiple(of: 3))
//OUTPUT : true
hasSuffix()
checks if “.jpg” is present at the end of the string “paris.jpg” in the filename variable and returns true if it is present.
Similarly, isMultiple(of:)
checks if the value 120 in the number variable is a multiple of 3 and returns true.
Boolean expressions cannot be subjected to arithmetic operations, so arithmetic operators cannot be used. But with Boolean expressions the !
operator is used, which means “NOT”. This operator reverses the value.
var isAuthenticated = false
isAuthenticated = !isAuthenticated
print(isAuthenticated)
//OUTPUT : true
isAuthenticated = !isAuthenticated
print(isAuthenticated)
//OUTPUT : false
There is a function toggle()
that can be used for bool expressions. This function inverts the value of the bool expression.
The toggle()
method does the same thing as the !
operator, it just makes it easier to read in long and complex code.
var gameOver = false
print(gameOver)
//OUTPUT : false
gameOver.toggle()
print(gameOver)
//OUTPUT : true
String Interpolation #
There are 2 methods for concatenating string expressions;
- Use of the
+
operator - String Interpolation
Using the + Operator #
The use of the +
operator with String is quite simple. You can combine two (or more) string expressions.
let firstPart = "Hello, "
let secondPart = "world!"
let greeting = firstPart + secondPart
print(greeting)
//OUTPUT : Hello, world!
We can continue to do this merging process one after the other.
let people = "Haters"
let action = "hate"
let lyric = people + " gonna " + action
print(lyric)
//OUTPUT : Haters gonna hate
We can even take things a bit further and do something like this.
let luggageCode = "1" + "2" + "3" + "4" + "5"
When you examine the above example, the last string expression we will have will be 12345
. But here we encounter a problem. Swift will not do this concatenation all at once. The path it will follow will be as follows:
“12”+”3”+”4”+”5”
“123”+”4”+”5”
“1234”+”5”
“12345”
So the concatenation will be done step by step. String concatenation with the +
operator is only suitable for low-step concatenations. But what if we need more? In that case, String Interpolation will come to the rescue.
String Interpolation #
Interpolation is quite simple to do. We can solve it with a backslash and 2 parentheses. Also Interpolation is not only String, it can combine Int
, Double
, Bool
and many other things with String.
let name = "Taylor"
let age = 26
let message = "Hello, my name is \(name) and I'm \(age) years old."
print(message)
//OUTPUT : Hello, my name is Taylor and I'm 26 years old.
As seen in the code above, we were able to insert an Int
value (age) directly into a String expression.
This is where the difference from the +
operator comes in. For example, if we were to write a code like the one below, this code would not work. Because only String expressions can be combined with the +
operator.
// CAUTION THIS CODE IS BUGGY. XCODE GIVES ERROR.
let number = 11
let missionMessage = "Apollo " + number + " landed on the moon."
But if we want to combine the above example with the +
operator, we can do the following.
let missionMessage = "Apollo " + String(number) + " landed on the moon."
But there is a faster way:
let missionMessage = "Apollo \(number) landed on the moon."
You can also read this article in Turkish.
Bu yazıyı Türkçe olarak da okuyabilirsiniz.