Getting Started
Variable
var score = 0 // Variable
let pi = 3.14 // Constant
var greeting = "Hello"
var numberOfToys = 8
var isMorning = true
var numberOfToys: Int = 8
numberOfToys += 1
print(numberOfToys)
// prints "9"
Type Annotations
var greeting: String = "Hello"
var numberOfToys: Int = 8
var isMorning: Bool = true
var price: Double = 8.99
Arithmetic Operators
+Add-Subtraction*Multiplication/Division%Remainder
var x = 0
x = 4 + 2 // x is now 6
x = 4 - 2 // x is now 2
x = 4 * 2 // x is now 8
x = 4 / 2 // x is now 2
x = 4 % 2 // x is now 0
+=Adds and assigns sums-=subtract and assign the difference*=Multiplication and assignment/=Divide and assign quotient%=Divide and assign remainder
Compound Assignment Operators
var numberOfDogs = 100
numberOfDogs += 1
print("There are \(numberOfDogs) Dalmatians!")
// print: There are 101 Dalmatians!
String Interpolation
Multi-line String
Code Comments
Form a Tuple
let player = ("Maya", 5, 150)
print(player) // ("Maya", 5, 150)
print("\(player.0): level \(player.1), \(player.2) pts") // Maya: level 5, 150 pts
Decompose Tuple
let player = (name: "Maya", level: 5)
let (currentName, curLevel) = player
print("\(currentName): level \(curLevel)")
// print: Maya: level 5
Special Comment Syntax
MARK
MARK can be used to display comments in the column
TODO
TODO is used to display reminders of things that need to be done
FIXME
FIXME is used to display reminders about things that need to be fixed