Learning Swift

Sat, May 22, 2021 3-minute read

My notes while studying Swift and iOS programming. Some of these are familiar and shared in modern languages.


Swift is the recommended language to develop for Apple devices. It is very similar to Kotlin (for Android) in many ways. So if you already know Kotlin, Swift is easy.

Variables and Values

Swift supports Type Inference. For example, if I create a variable and assign it a text, it becomes a String. Example of primitive types in Swift:

  • Bool
  • String
  • Int
  • Long
  • Double
  • Array<?> where ? is another type
  • Dictionary<String, Int> can be seen as a String indexed list
  • Any a generic type, akin of Object in Java and Any in Kotlin

For example, you can do:

var hello = "Hello, world!"
var age:Int = 39
var password:String? //default nil, i.e., null in Apple jargon

Note: to access the value of password above, you need to use !, e.g.:

print(password!) //print the assigned value of the password. 

Constants are defined with let:

let userId = 123456 //can't reassign the variable userId


print("Hello, world!")


Converting a variable type to another looks like this:

//convert String to Int
var aText:String = "123"
var aNumber = Int(aText) //this is an Int

//convert Int to String
var intValue = 123
var strValue = String(intValue)

//convert Double to Int
var doubleNumber:Double = 1.2
var intDouble = Int(doubleNumber) //this will be 1 - we lose precision

For, while, repeat

//Different ways to count from 1 to 10

for index in 1...10 { //In Kotlin we use ..
  print("Number: \(index)")

var counter = 1
while counter <= 10 {
  print("Number: \(counter)")
  counter = counter + 1

counter = 1
repeat {
  print("Number: \(counter)")
  counter = counter + 1
} while count <= 10

To stop a loop: break and to skip an iteration of a loop: continue

Array, Set, Dictionary


A collection of items.

var items = [1,2,3,4] //array of Int
for item in items {
  print("Item: \(item)")

//Swift is 0-indexed. e.g., modify the second (index = 1) element to 5.
items[1] = 5

//e.g., an empty mutable array of Int
var items = [Int]()
items.append(1) //items contains [1]
items.append(2) //items contains [1,2]

//e.g., an empty mutable array of any type
var mixed = [Any]()
mixed.append("Hello") //mixed contains [1,"Hello"]


A collection of unique items.

var elements = Set<String>()

elements will only contain Alice and Bob, even thought we inserted Bob twice.


A key-indexable collection of items.

var coins = [String:String]
elements["EUR"] = "Euro"
elements["USD"] = "US Dollar"
print(elements["EUR"]!) //prints "Euro"


func sum(number1:Int, number2:Int) -> Int {
  return number1 + number2