generics with swift

60
Generics with Swift by akira hirakawa @akirahrkw

Upload: hirakawa-akira

Post on 17-Jul-2015

108 views

Category:

Engineering


1 download

TRANSCRIPT

Generics with Swiftby akira hirakawa @akirahrkw

Do you Swift?

we use swift for Burpple app

Generics with Swift

Do you use Generics?

example

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

}

these are identical !!

more useful more flexible?

generics !!

Generic functionfunc swapTwoValues<T>(inout a: T, inout b: T) {

let temporaryA = a

a = b

b = temporaryA

}

swap Int valuesvar someInt = 3

var anotherInt = 107

swapTwoValues(&someInt, &anotherInt)

swap String valuesvar someString = "hello"

var anotherString = "world"

swapTwoValues(&someString, &anotherString)

can notvar someInt = 3

var anotherString = "world"

swapTwoValues(&someInt, &anotherString)

useful flexible

but

why not use Protocol?

can

swap two Any valuesfunc swapTwoValues(inout a: Any, inout b: Any) {

let temporaryA = a

a = b

b = temporaryA

}

var a = 1 as Any

var b = 2 as Any

swapTwoValues(&a, &b)

var a = “a” as Any

var b = “b” as Any

swapTwoValues(&a, &b)

but

var a = 1 as Any

var b = “a” as Any

swapTwoValues(&a, &b)

which is better?

what is the difference?

func executeProtocol(p: Printable) -> Printable {

}

func executeGenerics<T: Printable>(p: T) -> T {

}

almost same

func executProtocol(p: Printable) -> Printable {

p.description

return p

}

func executeGenerics<T: Printable>(p: T) -> T {

p.description

return p

}

no difference?

return value

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()

useful

different

performance?

case swap method:

case increment:

depends on -O and swift version

fast useful

flexible

in practice?

case DataSource:

but

case DataSource:

Generics doesn’t work on TableViewDeleate, TableViewDataSource because of a bug

ds.respondsToSelector(“tableView:numberOfRowsInSection:”) is false

http://www.openradar.me/radar?id=5829206453780480

case API Call:

Request<T>

Request<[User]>

Object Mapper

AnyObject -> Tvar orm = { (json: AnyObject) -> User in … … return user

}

var orm = { (json: AnyObject) -> Media in … … return media

}

Completion Handler

T -> ()var handler = { ( item:User) -> () in … …

}

var handler = { (item:Media) -> () in … …

}

fast useful

flexible practical

I like generics :)

thanks

bit.ly/tinyjson bit.ly/code20150416

question?