SlideShare a Scribd company logo
Generics with Swift
by akira hirakawa
@akirahrkw
Do you Swift?
we use swift for
Burpple app
Generics With Swift
Generics with Swift
Do you use Generics?
example
swap two Int values
func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
swap two Double values
func swapTwoDoubles(inout a: Double, inout b: Double) {
let temporaryA = a
a = b
b = temporaryA
}
swap two String values
func swapTwoStrings(inout a: String, inout b: String) {
let temporaryA = a
a = b
b = temporaryA
}
these are identical !!
more useful
more flexible?
generics !!
Generic function
func swapTwoValues<T>(inout a: T, inout b: T) {
let temporaryA = a
a = b
b = temporaryA
}
swap Int values
var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
swap String values
var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)
can not
var someInt = 3
var anotherString = "world"
swapTwoValues(&someInt, &anotherString)
useful
flexible
but
why not use
Protocol?
can
swap two Any values
func 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
}
-> Printable
you need to cast to use the return value
var movie: Movie = …
var p: Printable = executProtocol(movie)
(p as Movie).show()
-> T
you 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:numberOfRo
wsInSection:”) is false
http://www.openradar.me/radar?
id=5829206453780480
case API Call:
Request<T>
Request<[User]>
Object Mapper
AnyObject -> T
var 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?

More Related Content

PPT
150970116028 2140705
PPTX
Command line arguments
PPTX
Variadic functions
PPTX
Lecture 2. mte 407
PPTX
Function composition in Javascript
PPTX
String .h
PPTX
Curry functions in Javascript
PDF
Slides
150970116028 2140705
Command line arguments
Variadic functions
Lecture 2. mte 407
Function composition in Javascript
String .h
Curry functions in Javascript
Slides

What's hot (20)

PPT
Function
PDF
03 function overloading
PPTX
Mutability for good not evil
PPT
Review functions
PPTX
Recursion transformer
PPTX
Functions in c++,
PDF
10. funtions and closures IN SWIFT PROGRAMMING
DOC
Labsheet_3
PDF
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
PPTX
Function overloading
KEY
PPT
4 Type conversion functions
PPTX
Actor systems
PDF
Let's make a contract: the art of designing a Java API
PDF
Go serving: Building server app with go
ZIP
.Net 4.0 Threading and Parallel Programming
PDF
Intro python-object-protocol
PPT
Enumerable
PPTX
A Few Interesting Things in Apple's Swift Programming Language
Function
03 function overloading
Mutability for good not evil
Review functions
Recursion transformer
Functions in c++,
10. funtions and closures IN SWIFT PROGRAMMING
Labsheet_3
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
Function overloading
4 Type conversion functions
Actor systems
Let's make a contract: the art of designing a Java API
Go serving: Building server app with go
.Net 4.0 Threading and Parallel Programming
Intro python-object-protocol
Enumerable
A Few Interesting Things in Apple's Swift Programming Language
Ad

Viewers also liked (7)

PDF
Advanced Swift Generics
PDF
Generics programming in Swift
PDF
Swift Generics in Theory and Practice
PDF
Real World Generics In Swift
PPTX
Swift 0x17 generics
PDF
Adopting Swift Generics
PDF
Java Generics: a deep dive
Advanced Swift Generics
Generics programming in Swift
Swift Generics in Theory and Practice
Real World Generics In Swift
Swift 0x17 generics
Adopting Swift Generics
Java Generics: a deep dive
Ad

Similar to Generics With Swift (20)

PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
PDF
An introduction to functional programming with Swift
ODP
Function
PPTX
Unit_I-Introduction python programming (1).pptx
PPTX
What is c
PPTX
Python Conditional_Statements_and_Functions
PDF
46630497 fun-pointer-1
PPTX
Laziness, trampolines, monoids and other functional amenities: this is not yo...
PDF
Loops_in_Rv1.2b
PPTX
Introduction to golang
PPTX
UNIT – 3.pptx for first year engineering
PDF
FP in Java - Project Lambda and beyond
PDF
Introducing dimensional
PDF
Paolo Galeone - Dissecting tf.function to discover auto graph strengths and s...
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
PDF
Operator overloading
PDF
Swift 3.0 の新しい機能(のうちの9つ)
PPTX
05 operators
PPTX
Java: Regular Expression
If You Think You Can Stay Away from Functional Programming, You Are Wrong
An introduction to functional programming with Swift
Function
Unit_I-Introduction python programming (1).pptx
What is c
Python Conditional_Statements_and_Functions
46630497 fun-pointer-1
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Loops_in_Rv1.2b
Introduction to golang
UNIT – 3.pptx for first year engineering
FP in Java - Project Lambda and beyond
Introducing dimensional
Paolo Galeone - Dissecting tf.function to discover auto graph strengths and s...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Operator overloading
Swift 3.0 の新しい機能(のうちの9つ)
05 operators
Java: Regular Expression

More from Hirakawa Akira (7)

PDF
Function, Class
PDF
Swift Basics
PDF
Class vs struct for Swift
PDF
Swift Coding Style
PDF
5 random ruby tips
PDF
QXCameraKit
PDF
東南アジアでエンジニア
Function, Class
Swift Basics
Class vs struct for Swift
Swift Coding Style
5 random ruby tips
QXCameraKit
東南アジアでエンジニア

Recently uploaded (20)

PPTX
additive manufacturing of ss316l using mig welding
PPTX
Welding lecture in detail for understanding
PPTX
Construction Project Organization Group 2.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Digital Logic Computer Design lecture notes
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
additive manufacturing of ss316l using mig welding
Welding lecture in detail for understanding
Construction Project Organization Group 2.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Operating System & Kernel Study Guide-1 - converted.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
OOP with Java - Java Introduction (Basics)
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Digital Logic Computer Design lecture notes
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Foundation to blockchain - A guide to Blockchain Tech

Generics With Swift