Swift Notes
Basics
-
How to compile and run:
1
swift hello_world.swift
-
No semicolons (though allowed) and no main functions. But semicolons must be used to write multiple statements in a single line.
-
Use
let
to make a constant andvar
to make a variable. (Uselet
whenever possible) Plus, multiple variables can be declared in one line:var x = 0, y = 0
. -
Types are inferred. If we need to make the type explicit, add the type after: (In practice, we rarely need to use the type annotations.)
1 2
var implicitInt = 70 var explicitInt: Int = 70
-
Use
\()
to include values in a string:1 2 3 4 5 6
var foo = 17 print("hello world \(foo)") var optionalString: String? = "I exist" print(\(optionalString!)) // forced unwrapping, we are sure the value exists. And ! is required var assumedString: String! = "I definitely exist" print(\(assumedString))
-
Use
[]
for both arrays and dictionaries. A comma is allowed after the last element. -
Use
"""
for multiple-line strings:1 2 3 4
let foo = """ Hello World """
-
Arrays, dictionaries and sets:
1 2 3 4 5 6 7 8 9 10 11 12
var emptyArray = [String]() emptyArray.append("first") emptyArray += ["second", "third"] emptyArray.insert("fourth", at: 3) var emptyDictionary = [String: Float]() // Or below if the type can be inferred var emptyArray = [] var emptyDictionary = [:] var threeDoubles = Array(repeating: 0.0, count: 3) var shoppingList: [String] = ["Eggs", "Milk"] var shoppingListInferred = ["Eggs", "Milk"] var fooSet: Set<Int>
-
The for loop:
for foo in foos {}
.for (foo, bar) in foobarDictionary {}
.for i in 0..<4 {}
is equivalent tofor i in range(0, 4):
in python.for i in 0...4 {}
is equivalent tofor i in range(0, 5):
in python.for (index, value) in shoppingList.enumerated()
gives tuples: (0, foo), (1, bar)
-
Optionals: add
?
to indicate that the value might be missing:var optionalString: String? = "foo"
(it can later be set tonil
.) (The type is required to init a nil.) -
Use
??
to provide a default value for an optional:1 2 3
let foo: String? let bar: String = "Default" print(foo ?? bar)
-
Optional binding
1 2 3 4 5 6 7
if let constantName = someOptional { // if the value != nil // statements } ... if let theApartmentNumber = person.residence?.address?.apartmentNumber { // optional chaining }
-
Functions:
1 2 3 4 5 6 7 8
func foo(bar: String) -> String { return "hello \(bar)" } foo(bar: "Till") // A tuple can be used to return multiple values // Functions call also be returned (First-class functions) // _ bar: String means no argument label when being called
-
String methods:
.isEmpty
and.count
(no parentheses) (.count is for length)..lowercased()
.hasPrefix()
,.hasSuffix()
,contains()
-
By default, the
switch
in Swift doesn’t fall through, which means we don’t needbreak
. (If needed,fallthrough
can be added.) Multiple conditions are allowed:case "a", "e", "i", "o", "u"
. Range values are allowed:case 0...9
. -
@discardableResult func foo() -> String {}
means the return is discardable. -
Double(x)
converts the type to Double. -
.toggle()
toggles boolean values. -
The default type for a char is
String
:1 2
let a = "a" // String let b : Character = "b" // Character
-
Any
andas?
:1 2 3 4
var items: [Any] = [5, "Bill", 6.7, Dog()] if let firstItem = items[0] as? Int { // Any is generic. as? is used for conditional casting }
-
enum
:1 2 3 4
enum CompassPoint { case north, east, south, west } var compassHeading: CompassPoint = .west
-
guard
:1 2 3 4
guard let name = nameField.text else { show("No name to submit") return }
OOP
-
Use
init()
super.init
andself
to make a constructor (similar to python):1 2 3 4 5 6 7 8 9 10 11 12
class Foo: Bar { var foobar: String init(foobar: String, name: String) { super.init(name: name) self.foobar = foobar } override func fooFunc() } let foo = Foo(foobar: "hello", name: "world")
-
In a setter, the new value has the implicit name
newValue
. -
Use
===
and!==
to see if two references are referring to the same object. -
A convenience initializer is like a helper, which always calls another initializer in the same class.
convenience init()
-
mutating func
changes a property. -
willSet
anddidSet
are property observers.
References
-
The Swift Programming Language
-
App Development with Swift