Swift Basics: Whole Number, Decimal Number, Booleans, And String Interpolation Overview

Swift Basics: Whole Number, Decimal Number, Booleans, And String Interpolation Overview

ยท

6 min read

Hi All, As I told in the last post I have started a #100DaysOfSwiftUI challenge this is my learning from Day 2. where I have learned the Whole numbers and decimal numbers, Boolean values, and String interpolation in Swift. Let's go through it once:

Numbers

As we know there are 2 types of numbers in maths first one is Whole numbers and the second is Decimal numbers so to handle those 2 types in Swift also we can the numbers accordingly as Whole numbers are called Integers and Decimal numbers are called as Floating-point Numbers or Doubles.

So we define Integers and Doubles like this:

let age = 18
let pi = 3.14
let score = -2

let reallyBigNumber = 100_000_000_000

So the whole number can start from small numbers like 0 to spread to any numbers on positive numbers or negative numbers. So when we define a large number as shown in the last line of the code snippet we can make use of the underscore to keep numbers separate at required places so code will be easily readable and will be ignored by swift only numbers are stored for the variable.

Also, the decimal numbers are called Doubles. which can be of any size like 0.000000000001 or any small number you can think of can be stored as a double value.

we can perform any mathematical operation on the Integer and Doubles like addition, subtraction, multiplication, and division like this:

let score = 26
let addScore = score + 11
let subtractScore = score - 13
let multiplyScore = score * 12 
let divideScore = score / 10

let finalScore = score + addScore - SubtractScore * multiplyScore / divideScore
print(finalScore)

we can perform a single math operation to a complex operation which includes different math operations to be performed single line to get the final value.

let a = 1
let b = 2.39

print (a + b)

print( Double(a) + b)
print(a + Int(b))

we can perform math operations with both decimal and double values together but there is a catch that we can't operate right away swift won't allow running the code on line 2 of the code snippet shared due to Type safety: Swift won't allow us to mix different types of data by accident.

So If we want to perform any math operations then we will have to convert the variable so all the variables are of the same type. As shown in the code snippet we can change the variable a to type Double or change the b to type Int so we have the same type of data to perform operations.

Type safety

Type safety in Swift means once Swift has decided what data type a constant or variable holds, it must always hold that same data type.

var a = "Hello"
a = 12
a = "valid assignment"

So what it means is once we provide a string value to variable a then in future variable a only accepts the values of type string any other type of data will be rejected and Xcode will show error that invalid type of data assigned. line 2 throws an error but line 3 will work as expected as we are providing a string value to variable a.

Booleans

There is always a time where the answer can only be yes/no or true/false. So for such cases, we have a data type called Booleans which expected only 2 values which are either true or false anything else will be throwing an error. we define a boolean variable like this:

let goodDog = true
let gameOver = false

Boolean is the most simple form of data type in Swift or any programming language as it expects simple true or false values.

unlike other data types the Boolean doesn't have + or - operations (since we can't calculate what is true + true is ๐Ÿ˜…) so we have one operator ! which means not. This flips the value of boolean from true to false or false to true. Also, there is one functionality that does the same action called toggle().

let gameOver = false 
print(gameOver)

gameOver.toggle()
print(gameOver)

This line of code prints the false first then after toggle() it will print true. Similar can be performed using the ! operator as well like this:

let gameOver = false 
print(gameOver)

gameOver = !gameOver
print(gameOver)

Similar to the code snippet shared above this code also works the same way.

String Interpolation

Swift gives us two ways to join the Strings together: One is using the + operator and the second is with a technique called String Interpolation.

First, let's try the simple way of joining the two strings together that is using the + operator. Which is like this:

let name = "Coding Monkey"
let greeting = "Morning"

print("Hello " + name + ", Good " + greeting)

when we run the above code it will print Hello Coding Monkey, Good Morning. So as we can see in the case of numbers the + was adding the two variables but now in the case of string variables strings are getting joined together and this is called Operator Overloading - the ability for one operator such as + to perform different action based on how it is being used.

This technique is easy to use but we shouldn't use it too much. As each time Swift tries to join 2 strings together using + it has to make a new string out of them before continuing, and if you have lots of strings to be joined together then it's wasteful. Also, the + operator only works when we are trying to join 2 strings together if we want to add a number in between 2 strings then this operation will throw an error.

So swift has a better solution to fix those problems called String Interpolation which joins 2 strings efficiently also supports adding numbers or decimals to a string.

let name = "Coding Monkey"
let age = 22

let message = "Hello, My name is " + name + " and I'm " + age + " years old."

let message. = "Hello, My name is \(name) and I'm \(age) years old."
print(message)

As I mentioned earlier when we try to run this code Xcode will throw an error in line 3 saying combining strings and numbers are not valid. But If we remove line 3 and again run the code Xcode will print Hello, My name is Coding Monkey and I'm 22 years old.

So with string interpolation, it is easier to add integers or decimals or any other data type to the string, and string joining is also done more efficiently. As we know when we add backslash then add some data then swift pays attention to that value so when we provide the values with a backslash and then in variable name inside parenthesis like this \(variableName) swift will get that value from code and joins that value to the string. so with a backslash, we can even perform the math operations inside the string interpolation action only.

print(" 6 x 6 is \(6 * 6)")

so when we code runs it will print 6 x 6 is 36.

Conclusion

So I have learned on the Day 2 of 100 Days of SwiftUI that how to use Decimal, Whole numbers, and perform math operations on them. Then use Boolean numbers and flip that boolean value. Finally joining Strings together using the + operator or using the string interpolation technique.

Also, let me know if any mistakes are done in this blog/write-up I have done on Day 2 learning so I can fix my mistakes and improve my understandings.

Thank you.