Programming & Tech Tips for Everyone

Swift Tutorial for Beginners: A Comprehensive Guide [in 2022]

Welcome to Learn iOS Development & Swift

This is a comprehensive and free guide to Swift programming.

This course is designed for complete beginners.

The first step toward becoming an iOS developer is learning the Swift programming language.

If you have no previous experience with Swift or any other programming language, you’ve come to the right place!

Course Description

This completely free Swift programming course is split into logical chapters.

Each chapter consists of a theory backed up by great examples.

To learn Swift, you have to spend time repeating and understanding these examples yourself!

We start by defining what is programming and iOS development in the first place. You are going to learn what it takes and what to expect.

Then you are going to install the Xcode developer tools for writing iOS applications.

When the Xcode setup is completed, you are going to start writing your first programs!

Feel free to pick a chapter from the above list of chapters! I recommend starting from the introduction, that is, the very first chapter.

I hope you enjoy the course!

If you have feedback, feel free to shoot me an email at artturi@codingem.com.

Chapter Overview

Here is a quick chapter-by-chapter overview of the course.

This section will give you an idea of what the course is all about.

Chapter 1: Introduction to iOS Development

Before jumping into the course and coding, it is important to understand what is iOS development and programming.

This helps you set some expectations for the course and for becoming an iOS developer in general.

Swift is the main language to use when building iOS apps.

To put it short, you have to be prepared to spend hundreds of hours learning Swift programming language.

To give some perspective, I’d say about 95% of the time you should write code, and only 5% of the time watch tutorials or read theory.

This course is no exception.

If you only read this course through, it would probably take less than 3 hours to complete.

However, you need to carefully repeat the examples to understand how the code truly works and to get some hands-on experience.

Chapter 2: Setting Up Xcode

To build apps with Swift, you need a code editor where you write the programs.

To start learning Swift, you need a Mac and an app called Xcode.

In this chapter, we are going to install and set up the Xcode developer tools.

You are also going to hear an alternative way to write Swift code if you do not have access to a Mac or Xcode.

Chapter 3: Variables and Constants

This is the first programming chapter in this course.

We start off with the basics, that is, how to store values in your program.

In Swift, you can store values such as numbers or text into variables and constants.

To create a variable or constant, use the keywords var and let respectively.

let name = "Alice"
var age = 35

Chapter 4: Data Types

Every single piece of data you’ll ever encounter represents some data type.

In Swift, the basic data types are:

  1. String for text.
  2. Int for whole numbers.
  3. Double and Float for decimal numbers.
  4. Bool for truth values (boolean values).
let name: String = "Alice"
var age: Int = 35

let pi: Double = 3.141592
let e: Float = 2.712

var isBoring: Bool = false

Chapter 5: Math Operators

After learning about data types it is time to take a look at the basic math operators in Swift.

These operators are something you are most likely familiar with from elementary school.

  • + for addition
  • for subtraction
  • * for multiplication
  • / for division
var sum = 10 + 20
var difference = 50 - 30
var product = 5 * 6 * 7
var fraction = 10.0 / 3.0

Chapter 6: Comparison Operators

After learning about the math operators, it is time to learn about comparison operators.

Here is a table that summarizes the chapter really well.

OperatorNameDescription
==equalsChecks if two values are equal to one another.
!=not equalChecks if two values are not equal to one another.
<less thanChecks if the left-hand side is less than the value on the right-hand side.
<=less than or equalChecks if the left-hand side is less than or equal to the value on the right-hand side.
>greater thanChecks if the left-hand side is greater than the value on the right-hand side.
>=greater than or equalChecks if the left-hand side is greater than or equal to the value on the right-hand side.

Chapter 7: Logical Operators

Logical operators are the first step toward writing programs that can make decisions.

Logical operators are used to connecting logical expressions, that is, expressions that are either true or false.

In Swift, there are three logical operators:

OperatorNameDescriptionExample
&&Logical AND operator.If and only if both the operands are true, the condition is true.true && false = false
||Logical OR Operator.If either one of the two operands is true, the condition is true.true || false = true
!Logical NOT Operator.Reverses the logical state of the operand. If a condition is true, the Logical NOT makes it false.!true = false

Chapter 8: If-Else Statement

After learning about logical operators, it is time to build a program that can make decisions based on conditions.

In Swift, you can use if-else statements to accomplish this.

The idea is simple:

  • If a condition (logical expression) is true, do something.
  • If the condition is not true, do something else.
let age = 15

if age < 18 {
    print("You cannot drive")
} else {
    print("You can drive")
}

Chapter 9: Switch Statement

A common alternative to the if-else statements is a switch statement.

The switch statement can be used to make your code look cleaner when your if-else statements are really long.

Here is a verbose if-else mess:

let num = 3

if num == 0 {
    print("0th")
} else if num == 1 {
    print("1st")
} else if num == 2 {
    print("2nd")
} else if num == 3 {
    print("3rd")
} else {
    print(String(num) + "th")
}

And here is the version that uses the switch statement:

let num = 3

switch num {
    case 0: print("0th")
    case 1: print("1st")
    case 2: print("2nd")
    case 3: print("3rd")
    default: print(String(num) + "th")
}

Chapter 10: For Loops

Another really important concept in programming is iteration.

A program usually has to process loads of data. So much so that it would be infeasible to manually process it.

This is where loops are used.

In Swift, there are two types of loops:

  • For loops
  • While loops

This chapter is dedicated to for loops, and the next chapter is all about while loops.

for count in 1...5 {
    print(count)
}

Chapter 11: While Loops

While loop is the other loop type in Swift.

You can use while loop in similar tasks as for loops.

However, the operating principle and the syntax are a bit different.

var i = 1

while i <= 5 {
    print(i)
    i = i + 1
}

Chapter 12: Functions

Thus far you have written simple Swift scripts that perform some elementary tasks.

To write manageable code, you have to be able to reuse some parts of it.

This is where functions are used.

A function is a reusable piece of code that solves a problem. A function always has a name with which it can be called.

Furthermore, a function is usually called with different inputs to get different outputs.

func greet(name: String) {
    print("Hello", name)
}

greet(name: "Alice")
greet(name: "Bob")
greet(name: "Charlie")

Chapter 13: Structs for Custom Data Type

To this point, you have used built-in data types, such as Int, String, or Bool.

However, you can also implement your own custom types.

This is possible by defining a struct.

A struct is a code construct that lets you bundle together associated values.

For example, if you have variables name and age that are related to someone, you should group them up into a struct called Person.

Then you can create multiple Person objects with different properties.

struct Person {
    var name: String
    var age: Int
}

Chapter 14: Classes

In the previous chapter, you learned how to create a custom data type using structs. With a struct, you can create independent objects in your code.

In Swift, you can also use a class to create a custom data type and objects in your program.

As a rule of thumb, you should prefer structs.

But if you cannot get the job done with one, you can use a class.

This chapter focuses on classes and compares the differences to structs. You will also learn in which situations a class is a better option than a struct.

struct Pet {
    var name: String
    init(name: String) {
        self.name = name
    }
}

Chapter 15: Class Inheritance

One of the main differences between a class and a struct is inheritance.

With classes, you can create a child class that inherits the behavior and contents of a parent class.

In other words, every variable, constant, and method that belongs to a class will also belong to its subclass.

Inheritance is a way to logically group classes together to reuse parts of code without repetition.

class Pet {
    var name = "Luna"
    var owner = "Alice" 
}

// Inherit Pet to Cat class
class Cat: Pet {
    var lives = 9
}

Chapter 16: Optionals

In Swift, it is quite common to have a variable that can either store a value or not have a value at all.

However, a computer does not understand what is a valueless object. Because of this, a special object called nil is used for representing empty values.

In Swift, it is possible to create an object that is either a value or a nil.

This is called optional.

Optionals are known to confuse beginners as you have to deal with question marks and exclamation marks in your code.

// Creating an optional value
var ageOptional: Int? = 10

// Unwrapping an optional value
var ageUnwrapped = ageOptional!

Chapter 17: Arrays

Earlier in this course, you learned about the basic data types, such as String, Int, or Bool.

In these last two chapters you are going to learn about the really commonly used data types, that is, arrays and dictionaries.

An array is an ordered collection of values.

In other words, you can use an array to store multiple pieces of information in the same place.

With arrays, you can easily access, modify, and remove elements from them.

let numbers = [1, 2, 3, 4, 5]

Chapter 18: Dictionaries

Last but not least, you are going to learn how to store data with unique keys to a collection.

This type of collection is called a dictionary.

A dictionary consists of key-value pairs, where each key is a unique identifier to the value.

This sounds cryptic, but it is not.

For example, you could store students in a dictionary where the student ID is a key and the name of the student is the value.

let data = [324982: "Alice", 318902: "Bob", 410922: "Charlie"]

To get a value from a dictionary, you need to know the key to it.

This chapter is the last chapter in this course. At the end of this chapter, you are going to find the concluding chapter of this course with some information on where to go next.

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email