Copy func addTwoNumbers ( a : Int , b : Int ) -> Int {
return a + b;
}
print ( addTwoNumbers ( a : 5 , b : 6 ))
Copy class Rat {
var health = 5 ;
}
class Bplate {
let numberOfIngredientsICantPronounce = 50
var m_rat: Rat ?
func checkForRat ( exterminationInstruction : (Rat) -> Void ) {
if let rat = m_rat{
exterminationInstruction ( rat )
}
}
}
Functions as parameters...
Copy func exterminateRat ( rat : Rat) {
print ( "rip Remy" )
rat.health = 0
}
let myBplate = Bplate ()
myBplate.m_rat = Rat ()
myBplate. checkForRat ( exterminationInstruction : exterminateRat )
Copy myBplate. checkForRat ( exterminationInstruction : {
(rat : Rat) in //could specifiy return type too (rat: Rat) -> Void
print ( "this is why USC is the lamer school" )
rat.health = 0
} )
Copy class Bird {
func fly () { print ( "fly" ) }
}
class Swan : Bird {
func poop () {}
}
var myBird: Bird
myBird = Swan ()
if let forSureSwan = myBird as? Swan {
forSureSwan. fly ()
}
guard let data = response.result. value as? [ String : Any ] else {
print ( "could not parse JSON" )
return
}
Copy import Foundation
var str: String = "First Last"
var strArr = str. components ( separatedBy : " " )