- Swift Documentation (Official) (swift.or)
- Swift Programming Language (Official) (swift.or)
- One-Stop Quick Reference for Swift Developers (swiftly.dev)
Swift Cheat Sheet
This cheat sheet provides examples of using Swift that cover basic Swift knowledge, control flow etc.
Also See
Enumerate
Computed properties
enum ShirtSize: String {
case small = "S"
case medium = "M"
case large = "L"
case extraLarge = "XL"
var description: String {
return "The size of this shirt is \(self.rawValue)"
}
}
Initialize from primitive value
enum Hello: String {
case english = "Hello"
case japanese = "Hello!"
case emoji = "ð"
}
let hello1 = Hello(rawValue: "Hello!")
let hello2 = Hello(rawValue: "ÐÑивеÑ")
print(hello1) // Optional(Hello.japanese)
print(hello2) // nil
instance method
enum Traffic {
case light
case heavy
mutating func reportAccident() {
self = .heavy
}
}
var currentTraffic: Traffic = .light
currentTraffic. reportAccident()
// currentTraffic is now .heavy
Just like classes and structs, enumerations can have instance methods. If an instance method mutates the value of the enum, it needs to be marked mutating
Related values
enum Dessert {
case cake(flavor: String)
case vanillaIceCream(scoops: Int)
case brownie
}
let order: Dessert = .cake(flavor: "Red Velvet")
Original value
enum Beatle: String {
case john paul george ringo
}
print("The Beatles are \(Beatle.john.rawValue).")
// print: The Beatles are john.
CaseIterable
enum Season: CaseIterable {
case winter
case spring
case summer
case falls
}
for season in Season.allCases {
print(season)
}
Add conformance to the CaseIterable
protocol to access the allCases
property, which returns an array of all cases of the enumeration
Define the enumeration
enum Day {
case monday
case tuesday
case wednesday
case thursday
case friday
case saturday
case sunday
}
let casualWorkday: Day = .friday
class
This is an example of a struct definition and a class definition
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
The Resolution
structure definition and the VideoMode
class definition only describe the appearance of Resolution
or VideoMode
, create an instance of the structure or class:
let resolution = Resolution(width: 1920)
let someVideoMode = VideoMode()
Example
use data type
class Student {
var name: String
var year: Int
var gpa: Double
var honors: Bool
}
Use default property values
class Student {
var name = ""
var gpa = 0.0
var honors = false
}
Inherit
Suppose we have a BankAccount class:
class BankAccount {
var balance = 0.0
func deposit(amount: Double) {
balance += amount
}
func withdraw(amount: Double) {
balance -= amount
}
}
SavingsAccount
extends BankAccount
class
class SavingsAccount: BankAccount {
varinterest = 0.0
func addInterest() {
let interest = balance \*0.005
self. deposit(amount: interest)
}
}
The new SavingsAccount
class (subclass) automatically gets all the characteristics of the BankAccount
class (superclass). Additionally, the SavingsAccount
class defines an .interest
property and an .addInterest()
method.
Class Attributes
var ferris = Student()
ferris.name = "Ferris Bueller"
ferris.year = 12
ferris.gpa = 3.81
ferris.honors = false
instance of the class
class Person {
var name = ""
var age = 0
}
var sonny = Person()
// sonny is now an instance of Person
reference type (class)
class Player {
var name: String
init(name: String) {
self.name = name
}
}
var player1 = Player(name: "Tomoko")
var player2 = player1
player2.name = "Isabella"
print(player1.name) // Isabella
print(player2.name) // Isabella
structure
Structural methods
struct Dog {
func bark() {
print("Woof")
}
}
let fido = Dog()
fido.bark() // prints: Woof
Mutation method (mutating)
struct Menu {
var menuItems = ["Fries", "Burgers"]
mutating func addToMenu(dish: String) {
self.menuItems.append(dish)
}
}
Using the Menu
class
var dinerMenu = Menu()
dinerMenu.addToMenu(dish: "Toast")
print(dinerMenu.menuItems)
// prints: ["Fries", "Burgers", "Toast"]
Check type
print(type(of: "abc")) // print: String
print(type(of: 123)) // print: 123
init() method
struct TV {
var size: Int
var type: String
init(size: Int, type: String) {
self.size = size
self.type = type
}
}
Using the TV
class
var newTV = TV(size: 65, type: "LED")
Structural instance creation
struct Person {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self. age = age
}
}
// Person instance:
var morty = Person(name: "Peter", age: 14)
Default property values
struct Car {
var numOfWheels = 4
var topSpeed = 80
}
var reliantRobin = Car(numOfWheels: 3)
print(reliantRobin.numOfWheels) // prints: 3
print(reliantRobin.topSpeed) // print: 80
Structure Creation
struct Building {
var address: String
var floors: Int
init(address: String, floors: Int) {
self.address = address
self. floors = floors
}
}
Structs or structs are used to programmatically represent real-life objects in code. A structure is created using the struct
keyword, followed by its name, followed by a body containing its properties and methods
function
Optional parameters
func getFirstInitial(from name: String?) -> String? {
return name?.first
}
Functions can accept optional types and return optional types. When a function cannot return a reasonable instance of the requested type, it should return nil
variable parameter
func totalStudent(data: String...) -> Int {
let numStudents = data.count
return numStudents
}
print(totalStudent(data: "Denial", "Peter"))
// print: 2
Input and output parameters
var currentSeason = "Winter"
func season(month: Int, name: inout String) {
switch month {
case 1...2:
name = "Winter âï¸"
case 3...6:
name = "Spring ð±"
case 7...9:
name = "Summer â±"
case 10...11:
name = "Autumn ð"
default:
name = "Unknown"
}
}
season(month: 4, name: ¤tSeason)
print(currentSeason) // Spring ð±
Default parameters
func greet(person: String = "guest") {
print("Hello \(person)")
}
greet() // Hello guest
greet(person: "Aliya") // Hello Aliya
Implicit return
func nextTotalSolarEclipse() -> String {
"April 8th, 2024 ð"
}
print(nextTotalSolarEclipse())
// print: April 8th, 2024 ð
Parameters & Arguments
func findSquarePerimet(side: Int) -> Int {
return side \*4
}
let perimeter = findSquarePerimet(side: 5)
print(perimeter) // print: 20
// Parameter: side
// Argument: 5
return multiple values
func smartphoneModel() -> (name: String, version: String, yearReleased: Int) {
return ("iPhone", "8 Plus", 2017)
}
let phone = smartphoneModel()
print(phone.name) // print: iPhone
print(phone.version) // print: 8 Plus
print(phone.yearReleased) // print: 2017
Omit parameter labels
func findDiff(\_ a: Int, b: Int) -> Int {
return a -b
}
print(findDiff(6, b: 4)) // prints: 2
Multiple parameters
func convertFracToDec(numerator: Double, denominator: Double) -> Double {
return numerator / denominator
}
let decimal = convertFracToDec(numerator: 1.0, denominator: 2.0)
print(decimal) // prints: 0.5
return value
let birthYear = 1994
var currentYear = 2020
func findAge() -> Int {
return currentYear-birthYear
}
print(findAge()) // prints: 26
Call functions
func greetLearner() {
print("Welcome to QuickRef.ME!")
}
// function call:
greetLearner()
// print: Welcome to QuickRef.ME!
Basic functions
func washCar() -> Void {
print("Soap")
print("Scrub")
print("Rinse")
print("Dry")
}
dictionary
Traversing the dictionary
var emojiMeaning = [
"ð¤": "Thinking Face",
"ðª": "Sleepy Face",
"ðµ": "Dizzy Face"
]
// loop through keys and values
for (emoji, meaning) in emojiMeaning {
print("\(emoji) is called '\(meaning)Emoji'")
}
// iterate through keys only
for emoji in emojiMeaning. keys {
print(emoji)
}
// iterate through values only
for meaning in emojiMeaning. values {
print(meaning)
}
Assigning values to variables
var hex = [
"red": "#ff0000",
"yellow": "#ffff00",
"blue": "#0000ff",
]
print("Blue hexadecimal code \(hex["blue"])")
// print: blue hex code Optional("#0000ff")
if let redHex = hex["red"] {
print("red hexadecimal code \(redHex)")
}
// print: red hex code #ff0000
Assigning the value of a key-value pair to a variable will return an optional value. To extract values, use the optional expansion
Modify the key-value pair
var change = [
"Quarter": 0.29,
"Dime": 0.15,
"Nickel": 0.05
]
// Change the value using subscript syntax
change["Quarter"] = .25
// Change the value using .updateValue()
change. updateValue(.10, forKey: "Dime")
To change the value of a key-value pair, use the .updateValue()
method or the subscript syntax by appending brackets [ ]
with the existing keys within to the name of the dictionary, then adding the assignment operator (=
) followed by the modified value
Delete key-value pair
var bookShelf = [
"Goodnight": "Margaret Wise Brown",
"The BFG": "Roald Dahl",
"Falling Up": "Shel Silverstein",
"No, David!": "David Shannon"
]
// remove value by setting key to nil
bookShelf["The BFG"] = nil
// remove value using .removeValue()
bookShelf. removeValue(forKey: "Goodnight")
// remove all values
bookShelf. removeAll()
add to dictionary
var pronunciation = [
"library": "lai·breh·ree",
"apple": "a·pl"
]
// new key: "programming", new value: "prow gra"
pronunciation["programming"] = "prow·gra"
Initialize an empty dictionary
// initializer syntax:
var yearlyFishPopulation = [Int: Int]()
// Empty dictionary literal syntax:
var yearlyBirdPopulation: [Int: Int] = [:]
Initialize and populate the dictionary
var employeeID = [
"Hamlet": 1367,
"Horatio": 8261,
"Ophelia": 9318
]
Type Consistency
var numberOfSides = [
"triangle": 3,
"square": 4,
"rectangle": 4
]
Contains only String
keys and Int
values
Keys
var fruitStand = [
"Coconuts": 12,
"Pineapples": 12,
"Papaya": 12
]
Each key
is unique
even if they all contain the same value
Base Dictionary
var dictionaryName = [
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3"
]
An unordered
collection of pairs of data or key-value pairs
Arrays and collections
.subtracting() Subtraction
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D"]
var setC = setA.subtracting(setB)
print(setC)
// print: ["B", "A"]
.symmetricDifference() Symmetric difference
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D", "E", "F"]
var setC = setA.symmetricDifference(setB)
print(setC)
// print: ["B", "E", "F", "A"]
.union()
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D", "E", "F"]
var setC = setA.union(setB)
print(setC)
// print: ["B", "A", "D", "F", "C", "E"]
.intersection() Intersection
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D", "E", "F"]
var setC = setA.intersection(setB)
print(setC) // print: ["D", "C"]
.isEmpty property
var emptySet = Set<String>()
print(emptySet.isEmpty) // print: true
var populatedSet: Set = [1, 2, 3]
print(populatedSet.isEmpty) // print: false
Iterate over a collection
var recipe: Set = ["Egg", "Flour", "Sugar"]
for ingredient in recipe {
print ("Include \(ingredient) in the recipe")
}
.contains()
var names: Set = ["Rosa", "Doug", "Waldo"]
print(names.contains("Lola")) // print: false
if names.contains("Waldo"){
print("There's Waldo!")
} else {
print("Where's Waldo?")
}
// print: There's Waldo!
.remove() and .removeAll() methods
var oddNumbers: Set = [1, 2, 3, 5]
// remove existing element
oddNumbers.remove(2)
// remove all elements
oddNumbers.removeAll()
.insert()
var cookieJar: Set = [
"Chocolate Chip",
"Oatmeal Raisin"
]
// add a new element
cookieJar.insert("Peanut Butter Chip")
Populate the collection
var vowels: Set = ["a", "e", "i", "o","u"]
To create a set filled with values, use the Set
keyword before the assignment operator.
Empty collection (Set)
var team = Set<String>()
print(team)
// print: []
Collection (Set)
var paintingsInMOMA: Set = [
"The Dream",
"The Starry Night",
"The False Mirror"
]
We can use a collection (Set
) to store unique
elements of the same data type
Traverse the array
var employees = ["Peter", "Denial", "Jame"]
for person in employees {
print(person)
}
// print: Peter
// print: Denial
// print: Jam
.insert() and .remove() methods
var moon = ["ð", "ð", "ð", "ð"]
moon.insert("ð", at: 0)
// ["ð", "ð", "ð", "ð", "ð"]
moon. remove(at: 4)
// ["ð", "ð", "ð", "ð"]
.append() method and += operator
var gymBadges = ["Boulder", "Cascade"]
gymBadges.append("Thunder")
gymBadges += ["Rainbow", "Soul"]
// ["Boulder", "Cascade", "Thunder",
// "Rainbow", "Soul"]
Initialize with array literal
// use type inference:
var snowfall = [2.4, 3.6, 3.4, 1.8, 0.0]
// explicit type:
var temp: [Int] = [33, 31, 30, 38, 44]
index
The index refers to the item's position in the ordered list, and a single element is retrieved from the array using the subscript syntax array[index]
.
var vowels = ["a", "e", "i", "o", "u"]
print(vowels[0]) // prints: a
print(vowels[1]) // prints: e
print(vowels[2]) // print: i
print(vowels[3]) // prints: o
print(vowels[4]) // prints: u
Note: Swift arrays are zero-indexed, meaning the first element has index 0.
.count property
var grocery = ["ð¥", "ð¥", "ðª", "ð¥", "ð"]
print(grocery.count)
// print: 5
Array array
var scores = [Int]()
// array is empty: []
cycle
while loop
var counter = 1
var stopNum = Int. random(in: 1...10)
while counter < stopNum {
print(counter)
counter += 1
}
// loop to print until the stop condition is met
A while
loop accepts a condition and keeps executing its body code while the provided condition is true
. If the condition is never false, the loop will keep running and the program will get stuck in an infinite loop
Use underscores
for \_ in 1...3 {
print("Ole")
}
// print: Ole
// print: Ole
// print: Ole
break keyword
for char in "supercalifragilistic" {
if char == "c" {
break
}
print(char)
}
// print: s
// print: u
// print: p
// print: e
// print: r
continue keyword
for num in 0...5 {
if num % 2 == 0 {
continue
}
print(num)
}
// print: 1
// print: 3
// print: 5
The continue
keyword will force the loop to continue for the next iteration
for-in loop
for char in "hehe" {
print(char)
}
// print: h
// print: e
// print: h
// print: e
stride() function
for oddNum in stride(from: 1, to: 5, by: 2) {
print(oddNum)
}
// print: 1
// print: 3
scope
let zeroToThree = 0...3
//zeroToThree: 0, 1, 2, 3
Conditions
Simple guards
func greet(name: String?) {
guard let unwrapped = name else {
print("Hello guest!")
return
}
print("Hello \(unwrapped)!")
}
greet(name: "Asma") // output: Hello Asma!
greet(name: nil) // output: Hello guest!
Control the order of execution
// without parentheses:
true || true && false || false
//----> true
// with brackets:
(true || true) && (false || false)
//----> false
Combined Logical Operators
!false && true || false // true
!false && true
first evaluates and returns true
Then, the expression, true
|| false
evaluates and returns the final result true
false || true && false // false
true && false
first evaluates to return false
Then, the expression, false
|| false
evaluates and returns the final result false
Logical Operators
!true // false
!false //true
switch statement: where clause
let num = 7
switch num {
case let x where x % 2 == 0:
print("\(num) is even")
case let x where x % 2 == 1:
print("\(num) odd number")
default:
print("\(num) is invalid")
}
// print: 7 odd
switch statement: composite case
let service = "Seamless"
switch service {
case "Uber", "Lyft":
print("travel")
case "DoorDash", "Seamless", "GrubHub":
print("Restaurant delivery")
case "Instacart", "FreshDirect":
print("Grocery Delivery")
default:
print("Unknown service")
}
// print: restaurant takeaway
switch statement: interval matching
let year = 1905
var artPeriod: String
switch year {
case 1860...1885:
artPeriod = "Impressionism"
case 1886...1910:
artPeriod = "Post-Impressionism"
default:
artPeriod = "Unknown"
}
// print: post-impressionism
switch statement
var secondaryColor = "green"
switch secondaryColor {
case "orange":
print("A mixture of red and yellow")
case "purple":
print("A mix of red and blue")
default:
print("This may not be a secondary color")
}
// print: mix of blue and yellow
Ternary conditional operator
var driverLicense = true
driverLicense
? print("driver seat") : print("passenger seat")
// print: driver's seat
Comparison Operators
5 > 1 // true
6 < 10 // true
2 >= 3 // false
3 <= 5 // true
"A" == "a" // false
"B" != "b" // true
-<
less than
->
greater than
-<=
less than or equal to
->=
greater than or equal to
-==
is equal to
-!=
is not equal to
else if statement
var weather = "rainy"
if weather == "sunny" {
print("Get some sunscreen")
} else if weather == "rainy" {
print("Take an umbrella")
} else if weather == "snowing" {
print("Put on your snow boots")
} else {
print("Invalid weather")
}
// print: take an umbrella
else statement
var turbulence = false
if turbulence {
print("Please sit down.")
} else {
print("You are free to move around.")
}
// print: You are free to move around.
if statement
var halloween = true
if halloween {
print("Trick or treat!")
}
// print: Trick or treat!
if 5 > 3 {
print("5 is greater than 3")
} else {
print("5 is not more than 3")
}
// output: "5 is greater than 3"