Post on 17-Jul-2015
transcript
swap two Int valuesfunc swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
swap two Double valuesfunc swapTwoDoubles(inout a: Double, inout b: Double) {
let temporaryA = a
a = b
b = temporaryA
}
swap two String valuesfunc swapTwoStrings(inout a: String, inout b: String) {
let temporaryA = a
a = b
b = temporaryA
}
Generic functionfunc swapTwoValues<T>(inout a: T, inout b: T) {
let temporaryA = a
a = b
b = temporaryA
}
swap String valuesvar someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)
swap two Any valuesfunc swapTwoValues(inout a: Any, inout b: Any) {
let temporaryA = a
a = b
b = temporaryA
}
func executeProtocol(p: Printable) -> Printable {
}
func executeGenerics<T: Printable>(p: T) -> T {
}
func executProtocol(p: Printable) -> Printable {
p.description
return p
}
func executeGenerics<T: Printable>(p: T) -> T {
p.description
return p
}
func executProtocol(p: Printable) -> Printable {
return p
}
func executeGenerics<T: Printable>(p: T) -> T {
return p
}
-> Printableyou need to cast to use the return value
var movie: Movie = …
var p: Printable = executProtocol(movie)
(p as Movie).show()
-> Tyou don’t need to cast to use the value
var movie: Movie = …
movie = executeGenerics(movie)
movie.show()
Generics doesn’t work on TableViewDeleate, TableViewDataSource because of a bug
ds.respondsToSelector(“tableView:numberOfRowsInSection:”) is false
http://www.openradar.me/radar?id=5829206453780480
AnyObject -> Tvar orm = { (json: AnyObject) -> User in … … return user
}
var orm = { (json: AnyObject) -> Media in … … return media
}