Skip to content

Instantly share code, notes, and snippets.

@varokas
Last active August 29, 2015 14:13
Show Gist options
  • Save varokas/7c65f0359a7e8903f2d3 to your computer and use it in GitHub Desktop.
Save varokas/7c65f0359a7e8903f2d3 to your computer and use it in GitHub Desktop.
Swift cheatsheet

Assignment

let constant = "Constant cannot be changed"
var variable = "Variable can be changed later"

var x,y: Int
let x = 0, y = 0

Types

let i:Int = -1
let u:UInt = 99

let s:String = "A String"

let f:Float = 1.2
let d:Double = 6.7
let d = 6.7 //inferred to Double

let b:Boolean = true

let convert:Int = Int(1.2)

typealias Celcius = Int

Optional

var s?:String
s = "needle"
s = nil

let forced:String = s!

if let forced = s {
  println(forced)
} else {
  println("not found")
}

let forced:String = s ?? "default value" //if (s == nil) use default value

Tuples

let p1 = (1,2)
let p2 = (x:1, y:2)

let (x1,y1) = p1
let (x1,_) = p1
let x1 = p1.0, y1 = p1.1

let x = p2.x, y= p2.y

String

let str = "a string"

let emptyStr = String()
let stringSize = countElements(str)
let isEmpty = emptyStr.isEmpty

let c:Character = "a"
let charAt:Character = Array(str)[2]
for c in "string" {
    print(c)
}

let concat = "a" + "b"
let interpolation = "Value is \(str)"
let compare = (str == "another")

Array

var colors = [String]()
var colors:[String] = []
var colors:[String] = ["red","green","blue"]

colors += ["yellow"]
colors += ["orange", "purple"]
colors.append("blue")
colors.insert("cyan", atIndex: 1)
colors[0...1] = ["brown", "gray"] //Three dots

let aColor = colors[0]
let subColors = colors[1...2] //Three dots

print(subColors)

for color in colors {
}
for (index, color) in enumerate(colors) {
}

Dictionary

var capitals:[String:String] = ["France":"Paris", "Thailand": "Bangkok"]
var capitals = [String:String]()
var capitals:[String:String] = [:]

let cap:String = capitals["Thailand"]! //Dereferece to optionals

cap["England"] = "London" //add
cap["Thailand"] = nil //remove

let keys = [String](dict.keys)
let values = [String](dict.values)


for (key,value) in dict {
}

Loops

for i in 1..10 {
}
for var i = 1; i<=10; i++ {
}

Conditionals

let point = (1,1)
switch point {
  case (let x, 0): println("point on x with displacement of \(x)")
  case (0, _): println("point on y")
  case (1..5, 1..5): println("point within bounds")
  case let (x,y) where x == y: println("point is on line")
  case let (x,y): println("point out of bounds at \(x), \(y)")
}

Functions

func getBounds(numbers:[Int] = []) -> (lower: Int, upper:Int) {

}
func varArgs(numbers:Int...) {

}
func variableParameter(var i:Int) {
  //i can be changed, but no effect on the caller
}
func inoutParameter(inout i:Int) {
  //i can be changed, with effect on caller
}

//External parameters
func findDistance(aPoint p1: Point, anotherPoint p2: Point) -> [Double] {
}
func findDistance(#aPoint: Point, #anotherPoint: Point) -> [Double] {
}

Comments

//Single Line
/* Multiple line
   comment */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment