Learn Swift Language Basics
Basic introduction to how to get started with the Swift Programming language, this section is focused on explaining some Swift basic syntax and concepts such as variables, constants, types and more.

CONTENTS
3 mins
Variables and Constants
Variables and Constants are two different ways we can store values in swift using the var keyword for values that we intend to update later in our code and the let keyword for values that will never change once created and initialized.
Variables
Variables var are used for storing values that will change during the life cycle of a code (while code is running). They are created with the var keyword followed by name and value.
var person = "Joe"
var age = 16
print(person)
print(age)
// Update person & age variables
person = "Mike"
age = 17
print(person)
print(age)
Constants
Unlike variables Constants are used for storing values that will NOT change while your code is running, once they are initialized they cannot be reassigned a new value somewhere in your code. They can be created with the let keyword followed by name and value.
let name = "John Doe"
let age = 23
name = "Mike Jack" // not allowed
let city: String
city = "Toronto" // initialized
Type Annotations
In Swift, we can either create a variable or constant with or without a type annotation
and at the end both will yield the same result.
A type annotation tells Swift compiler what kind of value you intend
to store. name: type annotation
=> name: String
var country: String = "Canada"
var capital: String = "Ottawa"
var population21: Int = 38561490 // population as of Dec, 2021
let city: String
city = "Toronto"
type annotation
in the previous examples.
This is because Swift uses the power of type inference
to deduce the
type of variable we stored even when a type annotation is omitted.
This makes it unnecessary to always include a type annotation and therefore
making your code appear cleaner.Basic Types and Type Aliasing
Basic types are types that comes bundled in swift, for storing basic values such as Int, Float, Double, Boolean, and String
Boolean
Boolean types Bool
are used for storing True
or False
values
var isTorontoCapitalOfCanada: Bool = false
var isGameOver = 5 < 6 // True 5 is less than 6
print(isTorontoCapitalOfCanada)
print(isGameOver)
Int
Int is used for storing none decimal numbers (whole numbers)
such as -14
, 3800
and more. They can store hold values between
-2,147,483,648
and 2,147,483,647
even on 32 bit systems.
var age: Int = 15 // with type annotation Int
var address = 501
var currentScore = -41
Integer
such as Int32
, Int64
(for 32 and 64, bit systems)
and many others that can be used for string whole numbers, but Int
is the most
widely used and recommended by Swift engineers because of its ability to adapt to the native
system you are building on.Boolean
Boolean types Bool
are used for storing True
or False
values
var isTorontoCapitalOfCanada: Bool = false
var isGameOver = 5 < 6 // True 5 is less than 6
print(isTorontoCapitalOfCanada)
print(isGameOver)
Float
Unlike Int Float
is used for storing decimal numbers
(numbers with fractional components) such as -412.42
, 3.1415...
, price, speed and more.
- It can store numbers that are smaller or bigger than what
Int
can hold. - It represents a
32-bit
floating-point numbers. - It has a precision of 6 decimal digits.
var price: Float = 314.0 // with type annotation Float
var speed = 75.6 // km/s
// precision limit
var floatNumber: Float = 4.1495256784785415 // Output will be limited to 6 decimal digits
print(floatNumber)
Double
Double is everything Float is with the exception:
- It represents a
64-bit
floating-point numbers - It has larger precision than Float
- It has a precision of at least
16
decimal digits.
var price: Double = 314.0 // with type annotation Double
var speed = 75.6 // km/s
// precision limit
var doubleNumber: Double = 4.1495256784785415
print(doubleNumber)
Double
when not sure.String
You may have noticed that we have used the String type to store values previously.
The String types is used for storing a string of word(s), text(s) such as name, places etc. We will talk in-depth about strings later in the series.
let name = "Mike" // inferred type String
let country: String = "Canada"
Type Alias
typealias is simply a way of saying I want a type to be something different from its original name.
i.e. in your code if we want Int
to be called MagicNumber
you can easily do that with typealias
You can now replace Int
with MagicNumber everywhere you
could have used Int
typealias MagicNumber = Int // MagicNumber can now be used as Int
let myMagicNumber: MagicNumber = 105 // annotated type `MagicNumber <= Int`
print(myMagicNumber)
Basic Operators
Operators are special signs used for performing operations such as addition, subtraction
Assignment Operator =
We have used this type of operator times in this tutorial, we use it when we
want to assign a value to a variable or constant. i.e a = b
or a = 4
let a = 4
var person = "Jane"
swift
basics
863 Words
SERIES
Swift Introduction
Basic introduction to Swift Programming language basics.
3mins