Swift Cheatsheet
Contents
Basics
- Data Types
|
|
- Conditional Statements
|
|
- Loops
|
|
-
Use
letfor constants andvarfor variable (Usingletis always a good idea) -
Functions
|
|
OOP
-
Constructor or Initializer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14class Car: Vehicle { var color: String init(wheels: Int,color: String) { self.color = color super.init(wheels: wheels) } convenience init() { self.init(wheels: 4, color: "Red") } } let myCar = car() // use convenience init -
Protocols (similar to interface in Java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15protocol Runnable { func run() } class Human: Runnable { func run() { print("Human runs on two legs") } } class Cheetah: Runnable { func run() { print("Cheetah runs very fast!") } } -
Extensions
1 2 3 4 5 6 7extension Int { var squared: Int { return self * self } } let fourSquared = 4.squared // Outputs: 16