Finding a resource to study systematically is very hard. Especially if you are unfamiliar with the subject. I did the mistake of trying to learn Swift by watching App building tutorials. While I did understand the complete procedure, I was unable to figure out a way to build what I have in my mind. At this point, I have realized, I am doing it all wrong. I tried learning swift from various resources but I would soon get demotivated for various reasons.
Fortunately, I came across this website which is dedicated to teaching swift. The content is divided in the form of 100 days challenge which I am really excited about as I can either watch the video or read the content which is exactly the same.
I am starting my journey of learning Swift and hope that I will be able to keep up with the challenge. I will be posting my everyday learning activity as a story and will try to complete it in 100 consecutive days.
Let’s get started with Day 1.
Agenda
- Create constants and variables
- Creating Strings
- Store Whole Numbers
- Store Decimal Numbers
The course encourages you to download Xcode and practice everything using Swift Playgrounds. What you are learning might be small but it is critical for your success.
You can create a new playground by going to File -> New -> Playground after launching Xcode. Your default view after creating the playground will look like this.

Create Variables and Constants
Variables
var greeting = "Hello Playground"
var is used to declare greeting as a variable. That means the value of greeting can change any number of times throughout the code.
greeting is the variable name.
“Hello Playground” is the initial value assigned to the variable.
To change the value of greeting to “How are you?” just assign the value to the variable name. There is no need to use var over and over.
greeting = "How are you?"
Constants
If you have an element that doesn’t change the value throughout the code, swift encourages you to use constants. This will help you make less errors as your code grows.
let character = "Harry Potter"
Trying to change the value of the element will generate errors.
Creating Strings
Creating a string as a variable or constant is similar with slight difference of using var or let.
Wrap your value in double quotes and that value is automatically assumed as a String.
let movieName = "Movie name is Harry Potter"
You can include double quotes inside your value/text as well. Use backslash before using the quotes. For example, if I want my movieName value as Movie name is “Harry Potter”, the following code is used.
let movieName = "Movie name is \"Harry Potter\""
Multi-line Strings
In case you have decided to use multi-line strings, you are supposed to wrap the text in triple double quotes. Here is an example.
let movieName = """
Movie name
is
Harry Potter
"""
Ensure that your opening and closing quotes are in their own separate lines.
Length of a String
To get length of a string, you can use “.count”.
let movie = "Harry Potter"
print(movie.count)
You can also assign the result to a different variable or constant.
let movie = "Harry Potter"
let movieLength = movie.count
print(movieLength)
Both this methods will serve the same purpose in different scenarios.
Upper and Lower Case strings
You can view, update or assign to a new variable upper cased or lower cased of a given string.
var greeting = "Hello Playground"// Prints the upper and lower cased versions of greeting
print(greeting.uppercased())
print(greeting.lowercased())// Updating the value of greeting to upper cased version
greeting = greeting.uppercased()
Check Prefix and Suffix
To check if a given string has a certain prefix or suffix, we use .hasPrefix() or .hasSuffix()
let movie = "Harry Potter"print(movie.hasPrefix("Har"))
// Returns Trueprint(movie.hasPrefix("har"))
// Returns Falseprint(movie.hasSuffix("ter"))
//Returns False
Observe the first two print statements, we get different results because, swift is a highly case sensitive language.
Store Whole Numbers
This is pretty straight forward. You can assign a value to a variable or constant same as a string except we don’t use quotes.
var year = 2022
If you are assigning a large number to a variable, for better code readability, you can use underscores in middle of your number and swift will ignore it.
var year = 2_022
This will assign the value 2022.
Arithmetic Operations
All arithmetic operations are valid in swift. This can be approached in three ways.
- Assigning the value to a new constant or variable.
let year = 2022let lastYear = year - 1
2. Updating your value
let year = 2022year = year - 1
3. Using Compound Assignment Operators
let year = 2022year += 1 //Performs Additionyear -=1 //Performs Subtractionyear *= 2 //Performs Multiplication
Check Multiple
If you want to check if A is a multiple of B use the below example.
let A = 120
print(A.isMultiple(of:3))//orprint(120.isMultiple(of:3))
The result will be a boolean value.
Store Decimal Numbers
When you use a decimal number, swift considers it as double precision number.
let a = 0.1 //This is a double
Note that swift is a type safe language. You cannot combine two data types when performing arithmetic operations.
let a = 0.1
let b = 1
let c = a+b
This above code will generate error. In situations like this, you can use type conversion.
let a = 0.1
let b = 1let c = int(a) + blet d = a + Double(b)
Once you have assigned a variable a particular value, the data type of the variable cannot be changed later. For example, if you assign a string to variable, it cannot be changed to an integer later on. This will help you in avoiding mistakes.
That concludes our day 1 of learning Swift.
You may wonder why I am writing the same content that is available on the website. Well, I am doing this for two main reasons.
- To reflect on my understanding of the content that I have learned on that day.
- Challenging myself to write my study progress will keep me motivated.
I hope you liked today’s learning. See you tomorrow.