Swift Basics: Variables, Constants and Strings overview

Swift Basics: Variables, Constants and Strings overview

ยท

6 min read

Hi All, Recently I am trying to learn ios development as we know Swift is a must for ios development. So I have started following #100DaysOfSwiftUI from Paul Hudson on his website 100 Days of SwiftUI And I will be sharing my everyday learning as one blog / write up every day to share my progress and keep it for future relearning. Let's start with my learning of day 1 in 100 days of SwiftUI course.

Swift Variables

When we create apps we want to store some data like username or some data downloaded from the internet then we use variables to store that information that we use to display information in our app. Variables are building blocks of all programming languages. Everything else you do in building anything is just updating, storing, or manipulating those variables which are used to store the information. that information can be as simple as numbers, user names to complex values as complete Shakespeare's novels. Everything can be stored using the Variables.

In Swift, there are 2 ways to store the data. the first option is storing data that will be updated in the future such as age which will keep changing everyone(we keep getting old every year ๐Ÿ˜”) and the second option is a constant value that will never be changed such as user name or birthdate.

Variables

Let's try to understand how to define and use the data which can be/ will be updated in the future. and this option is the default way to store the variables.

var greeting = "Hello World!"
var age = 21

From the above code snippet, we can see that we are trying to store a text and number using 2 variables named title and age respectively. let's understand how to define a variable:

  • var in the beginning, tells Swift that we are defining a variable
  • Second is the name of that variable. (we can name the variable however the programmer wants but there are some restrictions on naming the variables but will come to that at a later point).
    • Always give the variable a descriptive name so you should be able to know what information you are storing just by reading its name
  • Then the equal sign indicates that whatever comes next will be the data we want to store.
  • Finally, the data we want to store can be of any type.
    • if we are storing a text type of data we have to keep the text inside the double quotes( " ") so swift will know where the data starts from and where it ends( Similar to the greeting variable shown in the code snippet)

Now let's try to update these stored values:

var playerName = "Monkey"
playerName= "Dog"
playerName = "Cat"

playerName = 12

So now we are creating a new variable named playerName which is used to store the player who is currently playing and as the game progresses different players will be playing the game then we will have to change the playerName variable to keep up to date on who is currently playing the game so as shown the next line we use the variable name then an equal sign (=) then we provide the updated player name who is currently playing like Dog and then cat so always the playerName variable will be having the player who is currently playing.

As you can see in the last line of the code snippet I trying to store number 12 in the playerName variable. but we will be getting an error message in swift if we try to do that in our code saying that the playerName variable requires a String type of data but we are trying to store a number there. we have this type-safe feature in swift so we shouldn't try to store different types of data by mistake while building our app and we don't break our code where we are expecting the playerName to string but if we provide a number then our app will crash ๐Ÿ˜…. So once the variable playerName is used to store String type of data while updating that variable we should always provide string type value to that variable.

Constants

Constants are used when we want to store data which should always remain the same and can't be modified in the future. like Name, birthdays, etc. creating a constant is similar to how we created a variable only change we do for constant is using the let in place of var like this:

let name = "Coding Monkey"
let birthYear = 1999

name = "Monkey Coder"

So when we are creating the Constant we replace the var with the let keyword and similar to the variable definition we are defining and storing the name and birth year.

In the last line of the code snippet, I am trying to update the name variable but if we put the same code in the Xcode it will throw an error message that the name variable is immutable and if you want to update the value change the let to var.

Strings

When we store text type of data then we call it String. A string can be from a single letter to a complete story of your liking.

Strings use double quotes to check the start and end of the text data we are trying to store which can be a short alphabetic text to emojis, punctuation, etc.

we define string data like this:

let filename = "paris.jpg"
let result = "โญ๏ธ You win! โญ๏ธ"

Several things can be done with string type of data such we define multi-line text to count the number of characters in a string or maybe changing all the text to uppercase using string manipulators.

we can define a multi-line string using the 3 double quotes at the start and end of the string.

let movie = """
A day in
the life of an
Apple engineer
"""

Let's try to count the number of characters and then we'll change all the text to upper case:

let name = "Coding Monkey"

let nameLength = name .count 

print(nameLength)

print(name.uppercased())

So if we want to print something in the console we have a feature called print in swift. If we want to print something in the Xcode we just have to put the content we have to print just like shown in the code snippet.

Also, we can create a variable or constant using the functions or some calculation done to assign the value similar to do in the second line in the code snippet.

Conclusion

So As Day 1 of 100 days of SwiftUI, I have learned to create variable constants and use the Strings in Swift.

If any mistakes are found in the blogs just let me know in the comments so I can fix my mistakes and improve my understanding also I will keep updating this blog as I continue my learning Swift and SwiftUI so I have an up-to-date blog that I can reference in future.