You don’t have to be great to start. But you have to start to be great.
Agenda
- Creating Booleans
- Joining strings together
- Checkpoint 1
Creating Booleans
Similar to creating other data types, creating a boolean is as simple as assigning a value.
let value = true
Creates a boolean of value true. It is important to note that all letters are lowercased.
You can use the result of another function to create a boolean. For example let value = 120.isMultiple(of: 3)
this will create a Boolean with initial value as true.
Using Not Operator
You can flip the value of a boolean using the not operator (!). Remember, if you wish to flip the value of a boolean, you should declare it as a variable.
var result = 120.isMultiple(of:3) //returns trueresult = !result //returns false
Using toggle()
Another way of flipping the value of a Boolean is to use toggle().
var result = 120.isMultiple(of:3) //trueresult.toggle() //falseresult //false
Unlike other data types, the second line in the code, will change the original value of the variable. Example
var movie = "Harry Potter" // "Harry Potter"movie.uppercased() // "HARRY POTTER"movie //"Harry Potter"
In this case, the original value remains unchanged.
Note: You cannot assign the toggle value of a boolean to another variable to the variable itself.
var test = truetest = test.toggle() // Throws errorvar test1 = test.toggle() //Gives the result as ()
Join Strings Together
Using + operator
let first = "one"
let second = "two"
let third = "three"
let result = first + second + third //"onetwothree"
let result2 = first + second + third + "four" //"onetwothreefour"
You can also use compound assignment operator if you are using variables.
var first = "Hello"first += "World" //"HelloWorld"
If you are using multiple string joins in one line, swift cannot perform all the operations at the same time. Instead, it goes from first to last in sequential order creating temporary strings.
let code = "1" + "2" + "3" + "4"
code //"1234"
To perform this operation, swift will create temporary strings as follows:
"1" + "2" -> "12"
"12" + "3" -> "123"
"123" + "4" -> "1234"
Though this functionality is useful, it takes up lot of memory.
When using + operator, all the values must be of type string let newstr = "1" + 2 +"3"
will throw error as 2 is of type Int. As a work around, you can use type conversion and perform like this let newstr = "1" + String(2) +"3"
.
String Interpolation
To include the value of a variable or constant, you can use syntax \(var_name)
.
This is called as String interpolation.
Example
let name = "Krishna"
let age = 25
let intro = "Hello! My name is \(name) and I'm \(age) years old"
You can also perform arithmetic operations.
let name = "Krishna"
let age = 20
let intro = "Hello! My name is \(name) and I'm \(age + 5) years old"
Checkpoint 1
Task
- Use a constant to store temperature in Celsius
- Convert the value into Fahrenheit by multiplying by 9, dividing by 5 and adding 32.
- Use string Interpolation to print the result
Solution
let tempCelsius = 25.2let tempFahrenheit = "The temperature \(tempCelsius) in Fahrenheit is \((tempCelsius * 9 /5) +32)"
I hope you enjoyed today’s learning. See you tomorrow.