While loops

While loops take the form:
while condition {
statement
}

var timer = 10
while timer > 0 {
	--timer
}

var beerVolume = 12.0
var sip = 0.3

while beerVolume > 0 {
	print("Cheers!")
	beerVolume -= sip
} 

Repeat-While loop
repeat {
statement
} while condition

repeat {
	--timer
} while timer > 0

repeat {
	print("Cheers")
	beerVolume -= sip
} while beerVolume > 0

Sets

var cutlery: Set = ["fork", "knife", "spoon", "spoon"]
var flowers:Set<Character> = ["","",""]

var utensils = Set<String>()
var trees = Set<Character>()

Control Flow
-for loop statement
-switch statement
-if else statement

For-in loops take the form:

for item in Collection {
statements to execute on each item
}

import UIKit
import Foundation

for var index = 99; index > 0; --index {
	print("\(index) bottles of beer on the wall. \(index) bottles of beer. Take one down, pass it around ...")
}
var demoString = "Swift enumeration is so fast!"
for character in demoString.characters {
	print(character)
}
let intArray = [7, 21, 25, 13, 1]
var sum = 0
for value in intArray {
	sum += value
}
var movieDict = ["star wars":"Geroge Lucas", "Point Break": "Kathryn Biegelow", "When Harry Met Sally": "Rob Reiner", "The Dark Knight": "Christopher Nolan"]

for (movie, director) in movieDict {
	print("\(director) directed \(movie)")
}

var animalGroupsDict = ["whales":"pod", "geese":"flock", "lions":"pride"]
for (animals, animalGroup) in animalGroupsDict {
	print("Many \(animals) form a \(animalGroup).")
}

Dictionary

Dictionary initialization

var groupsDict = [String:String]()
var animalGroupsDict = ["whales":"pod", "geese":"flock", "lions":"pride"]

var averageLifeSpanDic = [String:Range<Int>]()
var lifeSpanDic = ["African Grey Parrot":50...70, "Tiger Salamander":12...15, "Bottlemose Dolphin":20...30]

Dictionary operation

animalGroupsDict["crows"] = "murder"
animalGroupsDict["monkey"] = "troop"
animalGroupDict.count
println(animalGroupDict)

animalGroupDict["crows"] = nil
animalGroupDict

animalGroupsDict["monkeys"] = "barrel"
var group = animalGroupsDict.updateValue["gaggle", forKey: "geese"]
group.dynamicType

animalGroupsDict.updateValue("crash", forKey: "rhioceros")
println(animalGroupsDict)

if let groupOfWhales = animalGroupsDict["whales"]{
	println("We saw a \(groupOfWhales) of whales from the boat.")
} else {
	println("No value found for that key.")
}

Collections

[String:Character]()
lessons.append(“Collections”)

Set, Array, Dictionary

import UIKit
import Foundation

var numbers = Array<Double>()
var moreNumbers = [Double]()
moreNumbers = [85.0, 90.0, 95.0]

let differentNumbers = [97.5, 98.5, 99.0]
differentNumbers = moreNumbers + differentNumbers

var circuit = [livingRoomsSwitch, kitchenSwitch, bathroomSwitch]
var roadTripMusic = ["Neil Young", "Kendrick Lamar", "Flo Rida", "Nirvana"]

roadTripMusic.append("Rae Sremmurd")
roadTripMusic.insert("Dej Loaf", atIndex: 2)
roadTripMusic.removeIndex(3)
roadTripMusic.insert("Keith Urban", atIndex: 3)
roadTripMusic.count

let musician = roadTripMusic[2]

The Optional Data Type

Int, String, UIButton, AnyObject

Exclamation!
declare -var x:Int!
unwrap -x!

let w = Int("123")
w * 2

class BetterViewController: UIViewController {
	//var myButton: UIButton
}
import UIKit

class ViewController: UIViewController {
	var button:UIButton!

	override func viewDidLoad(){
		super.viewDidLoad()
		var title = button.titleForState(UIControlState.Normal)
	}
}
import UIKit

class Beverage {
	var category:String
	init (category: String){
		self.category = category
	}
}

class HotDrink: Beverage {
	var pairing:String

	init(category: String, pairing: String){
		self.pairing = pairing
		super.init(category: category)
	}
}

class ColdDrink: Beverage {
	var vessel:String

	init (category: String, vessel: String){
		self.vessel = vessel
		super.init(category: category)
	}
}

Optional

import UIKit
var x: Int
x = nil

var c: UIColor
c = UIColor.redColor()
c = nil

The method, .toInt(), is deprecated in the String struct. We now use the initializer method, Int(_: String), from the Int struct.

var y: Int
var s1: String
var s2: String

s1 = "123"
s2 = "ABC"

y = Int(s1)
y = Int(s2)

class ViewController: UIViewController {
	var button: UIButton
}

Constants and Variables

import UIKit
let encrouragement = "You can do it!"

var personalizedEncouragement = "You can do it, Lauren!"
personalizedEncouragement = personalizedEncouragement.stringByReplacingOccurrencesOfString("Lauren", withString:"Cameron")

let birthYear = 2008
var currentYear == 2017
var age = currentYear - birthYear

let buildingCapacity = 300
var attendance = 220
attendance += 2

var goat = UIImage(named:"Chinese-New-Year-3.jpg")!
let yearsOfTheGoat = [1967, 1979, 1991, 2003, 2015]
let yearsOfTheSheep = [1967, 1979, 1991, 2003, 2015]
yearsOfGoat.append(2027)

String

import UIKit
import Foundation

var funWithStrings = UIImage(named:"kittenWithOrangeString.jpg"):

let myFirstString = "no "
let mySecondString = "no problems"

let theTruth = myFirstString + ", " + mySecondString

var doggyDiet = "Lulu eats 25lbs of dog food per month"
var dogName = "Ferris"
var ferrisPic = UIImage(named:"SpringerdoodleFerris.jpg")
var lbsPerDay = 0.75
var daysPerMonth:Double = 30.0
doggyDiet = "\(dogName) eats \(lbsPerDay * daysPerMonth) of dog food per month"

var frankiePic = UIImage(named:"frankie.jpeg")!
lbsPerDay = 0.25
dogName = "Lil Frankie"
doggyDiet = "\(dogName) eats \(lbsPerDay * daysPerMonth) of dog food per month"

access an array of characters

var password = "Meet me in St. Louis"
for character in password.characters {
	if character == "e" {
		print("found an e!")
	} else {
		
	}
}

if else

if hungry && !vegetarian {
	println("Let's eat steak!")
} else if hungry && vegetarian {
	println("How about pumpkin curry?")
} else {
	println("nevermind")
}
var thereIsPie = true
if hungry || thereIsPie {
	println("Let's eat!")
} else {
	println("Let's wait.")
}

Ternary conditional

if question {
	answer1
} else {
	answer2
}

hugry ? println("Let's eat!") : println("Let's wait.")
hugry || thereIsPie ? println("Let's eat!") : println("Let's wait.")