SlideShare a Scribd company logo
Protocol-Oriented
Programming
The *correct* way to use Swift
What’s Protocol-Oriented?
Protocols in Swift
• Mostly the same with @protocols in Objective-C
• Protocols in Swift can be extended like a class extension in
Obj-C (protocol extension)
• Protocol extensions can have implementation code
• Made more powerful by generics in Swift
Advantages
• You solve problems related to inheritance
• You get rid of implicit sharing
• You end up with more readable code
Isn’t inheritance a good
thing?
Composition vs Inheritance Principle
class Dog {
func bark() {
print("Bark!")
}
}
class GermanShephard: Dog {
}
let myDog = GermanShephard()
myDog.bark() // prints "Bark!"
Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
Composition vs Inheritance Principle
class Dog {
func bark() {
print("Bark!")
}
}
class GermanShephard: Dog {
func sniffDrugs() {
if drugs {
bark()
}
}
}
class BelgianMalinois: Dog {
func sniffDrugs() {
if drugs {
bark()
}
}
}
class Poodle: Dog {
}
Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
Composition vs Inheritance Principle
class Dog {
func bark() {
print("Bark!")
}
}
class DrugSniffingDog: Dog {
func sniffDrugs() {
if drugs {
bark()
}
}
}
class GermanShephard: DrugSniffingDog {
}
class BelgianMalinois: DrugSniffingDog {
}
class Poodle: Dog {
}
Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
Composition vs Inheritance Principle
class Dog {
func bark() {
print("Bark!")
}
}
class DrugSniffingDog: Dog {
func sniffDrugs() {
if drugs {
bark()
}
}
}
class GermanShephard: DrugSniffingDog {
}
class BelgianMalinois: DrugSniffingDog {
}
class Poodle: Dog {
}
Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
Composition vs Inheritance Principle
protocol Barker {
func bark()
}
protocol Swimmer {
func swim()
}
protocol DrugSniffer {
func sniffDrugs()
}
extension Barker {
func bark() {
print("Bark!")
}
}
extension Swimmer {
func swim() {
print("Splash!")
}
}
extension DrugSniffer {
func sniffDrugs() {
print("I found drugs!");
}
}
class GermanShephard: Barker, DrugSniffer {
}
class BelgianMalinois: Barker, Swimmer, DrugSniffer {
}
class Poodle: Barker, Swimmer {
}
let myDog = BelgianMalinois()
myDog.bark() // prints "Bark!"
myDog.swim() // prints "Splash!"
myDog.sniffDrugs() // prints "I found drugs!"
Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
Isn’t sharing a good thing?
Bad sharing
class Temperature {
var celsius: Double = 0
var fahrenheit: Double {
get { return celsius * 9 / 5 + 32 }
set { celsius = (newValue - 32) * 5 / 9 }
}
}
Taken from https://developer.apple.com/videos/play/wwdc2015/414/
Bad sharing
let home = House()
let temp = Temperature()
temp.fahrenheit = 75
home.thermostat.temperature = temp
temp.fahrenheit = 425
home.oven.temperature = temp
home.oven.bake()
Taken from https://developer.apple.com/videos/play/wwdc2015/414/
Bad sharing
Taken from https://developer.apple.com/videos/play/wwdc2015/414/
House
Oven
Thermostat
Temperature
Bad sharing
struct Temperature: Equatable {
var celsius: Double = 0
var fahrenheit: Double {
get { return celsius * 9 / 5 + 32 }
set { celsius = (newValue - 32) * 5 / 9 }
}
}
func ==(lhs: Temperature, rhs: Temperature) -> Bool {
return lhs.celsius == rhs.celsius
}
Taken from https://developer.apple.com/videos/play/wwdc2015/414/
Bad sharing
let home = House()
var temp = Temperature()
temp.fahrenheit = 75
home.thermostat.temperature = temp
temp.fahrenheit = 425
home.oven.temperature = temp
home.oven.bake()
Taken from https://developer.apple.com/videos/play/wwdc2015/414/
Bad sharing
Taken from https://developer.apple.com/videos/play/wwdc2015/414/
House
Oven
Thermostat
Temperature
Temperature
–Heraclitus (Panta Rhei “Everything Flows”)
“Ever-newer waters flow on those who
step into the same rivers.”
jobs@smartwave.ph

More Related Content

PPTX
PDF
VR presentation APKOMINDO 2016
PDF
An Introduction to Groovy for Java Developers
PDF
Advanced debugging  techniques in different environments
PDF
Stepping into Scala
PDF
Go 1.10 Release Party - PDX Go
PPTX
IntroML_2.
VR presentation APKOMINDO 2016
An Introduction to Groovy for Java Developers
Advanced debugging  techniques in different environments
Stepping into Scala
Go 1.10 Release Party - PDX Go
IntroML_2.

Similar to Protocol-Oriented Programming in iOS: the Correct Way to Use Swift by JC Velasquez (20)

PDF
Semantic web assignment 2
PPT
Intro Java Rev010
PDF
Making%20R%20Packages%20Under%20Windows
PDF
Making%20R%20Packages%20Under%20Windows
PPT
Bioinformatica 29-09-2011-p1-introduction
PPTX
Lab1GoBasicswithgo_foundationofgolang.pptx
PDF
OSCON2014 : Quick Introduction to System Tools Programming with Go
PPT
Interfaces & Abstract Classes
PDF
Transforming WebSockets
PDF
Distro Recipes 2013 : Contribution of RDF metadata for traceability among pro...
PDF
Presentation distro recipes-2013
PDF
JavaScript guide 2020 Learn JavaScript
PDF
Composer for Busy Developers - php|tek13
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
PDF
Sugar Presentation - YULHackers March 2009
PDF
Go language presentation
PPTX
The GO Language : From Beginners to Gophers
PDF
Dependent things dependency management for apple sw - slideshare
PPTX
Dart, unicorns and rainbows
PPTX
Packaes & interfaces
Semantic web assignment 2
Intro Java Rev010
Making%20R%20Packages%20Under%20Windows
Making%20R%20Packages%20Under%20Windows
Bioinformatica 29-09-2011-p1-introduction
Lab1GoBasicswithgo_foundationofgolang.pptx
OSCON2014 : Quick Introduction to System Tools Programming with Go
Interfaces & Abstract Classes
Transforming WebSockets
Distro Recipes 2013 : Contribution of RDF metadata for traceability among pro...
Presentation distro recipes-2013
JavaScript guide 2020 Learn JavaScript
Composer for Busy Developers - php|tek13
2007 09 10 Fzi Training Groovy Grails V Ws
Sugar Presentation - YULHackers March 2009
Go language presentation
The GO Language : From Beginners to Gophers
Dependent things dependency management for apple sw - slideshare
Dart, unicorns and rainbows
Packaes & interfaces
Ad

More from DEVCON (20)

PPTX
Open Minded? Software Engineer to a UX Engineer. Ask me how. by Micael Diaz d...
PDF
The A1 by Christian John Felix
PPTX
Developing Your First Mobile VR App by NJ Realubit
PPTX
Smart Waste Disposal System by Russ Earl Malangen
PDF
Progressive Web Apps by Millicent Convento
PDF
How to Prevent Design Blindness by Tin Balabat
PDF
Payment Acceptance and Card Tokenization in JavaScript by Diwa Del Mundo
PDF
Solving Database Management, Migration, and Scaling Problems with DevOps Tool...
PDF
Securing Your AWS Cloud Infrastructure by Neil Hermosilla
PDF
Talk nerdy to me: how the future of UX is conversation and bots by Brian Rowe
PDF
Pokemon Go Analysis by Jolo Balbin
PDF
Docker - Contain that Wild Application by Marvin Arcilla
PDF
Applying Machine Learning for Mobile Games by Neil Patrick Del Gallego
PPTX
Quick prototyping (Construct 2 & Unity) by Roan Contreras
PDF
A Smarter World: The Mesh of Interconnected Devices and Artificial Intelligen...
PPTX
Creating a Hospital Based IoT Solution by Russ Earl Malangen
PDF
Developing a Smart Farm: Using Low-Cost electronics and a Civil Engineering B...
PPTX
Rain Classifier: The Engineered Way of Evaluating the Rain by Paulo Luis Lozano
PDF
Fundamentals of IoT: Communications with Uttr by Edmandie Samonte
PDF
App Store Optimization 101 by James Chua
Open Minded? Software Engineer to a UX Engineer. Ask me how. by Micael Diaz d...
The A1 by Christian John Felix
Developing Your First Mobile VR App by NJ Realubit
Smart Waste Disposal System by Russ Earl Malangen
Progressive Web Apps by Millicent Convento
How to Prevent Design Blindness by Tin Balabat
Payment Acceptance and Card Tokenization in JavaScript by Diwa Del Mundo
Solving Database Management, Migration, and Scaling Problems with DevOps Tool...
Securing Your AWS Cloud Infrastructure by Neil Hermosilla
Talk nerdy to me: how the future of UX is conversation and bots by Brian Rowe
Pokemon Go Analysis by Jolo Balbin
Docker - Contain that Wild Application by Marvin Arcilla
Applying Machine Learning for Mobile Games by Neil Patrick Del Gallego
Quick prototyping (Construct 2 & Unity) by Roan Contreras
A Smarter World: The Mesh of Interconnected Devices and Artificial Intelligen...
Creating a Hospital Based IoT Solution by Russ Earl Malangen
Developing a Smart Farm: Using Low-Cost electronics and a Civil Engineering B...
Rain Classifier: The Engineered Way of Evaluating the Rain by Paulo Luis Lozano
Fundamentals of IoT: Communications with Uttr by Edmandie Samonte
App Store Optimization 101 by James Chua
Ad

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectral efficient network and resource selection model in 5G networks
Programs and apps: productivity, graphics, security and other tools
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Protocol-Oriented Programming in iOS: the Correct Way to Use Swift by JC Velasquez

  • 3. Protocols in Swift • Mostly the same with @protocols in Objective-C • Protocols in Swift can be extended like a class extension in Obj-C (protocol extension) • Protocol extensions can have implementation code • Made more powerful by generics in Swift
  • 4. Advantages • You solve problems related to inheritance • You get rid of implicit sharing • You end up with more readable code
  • 5. Isn’t inheritance a good thing?
  • 6. Composition vs Inheritance Principle class Dog { func bark() { print("Bark!") } } class GermanShephard: Dog { } let myDog = GermanShephard() myDog.bark() // prints "Bark!" Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
  • 7. Composition vs Inheritance Principle class Dog { func bark() { print("Bark!") } } class GermanShephard: Dog { func sniffDrugs() { if drugs { bark() } } } class BelgianMalinois: Dog { func sniffDrugs() { if drugs { bark() } } } class Poodle: Dog { } Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
  • 8. Composition vs Inheritance Principle class Dog { func bark() { print("Bark!") } } class DrugSniffingDog: Dog { func sniffDrugs() { if drugs { bark() } } } class GermanShephard: DrugSniffingDog { } class BelgianMalinois: DrugSniffingDog { } class Poodle: Dog { } Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
  • 9. Composition vs Inheritance Principle class Dog { func bark() { print("Bark!") } } class DrugSniffingDog: Dog { func sniffDrugs() { if drugs { bark() } } } class GermanShephard: DrugSniffingDog { } class BelgianMalinois: DrugSniffingDog { } class Poodle: Dog { } Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
  • 10. Composition vs Inheritance Principle protocol Barker { func bark() } protocol Swimmer { func swim() } protocol DrugSniffer { func sniffDrugs() } extension Barker { func bark() { print("Bark!") } } extension Swimmer { func swim() { print("Splash!") } } extension DrugSniffer { func sniffDrugs() { print("I found drugs!"); } } class GermanShephard: Barker, DrugSniffer { } class BelgianMalinois: Barker, Swimmer, DrugSniffer { } class Poodle: Barker, Swimmer { } let myDog = BelgianMalinois() myDog.bark() // prints "Bark!" myDog.swim() // prints "Splash!" myDog.sniffDrugs() // prints "I found drugs!" Taken from http://mikebuss.com/2016/01/10/interfaces-vs-inheritance/
  • 11. Isn’t sharing a good thing?
  • 12. Bad sharing class Temperature { var celsius: Double = 0 var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } } Taken from https://developer.apple.com/videos/play/wwdc2015/414/
  • 13. Bad sharing let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake() Taken from https://developer.apple.com/videos/play/wwdc2015/414/
  • 14. Bad sharing Taken from https://developer.apple.com/videos/play/wwdc2015/414/ House Oven Thermostat Temperature
  • 15. Bad sharing struct Temperature: Equatable { var celsius: Double = 0 var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } } func ==(lhs: Temperature, rhs: Temperature) -> Bool { return lhs.celsius == rhs.celsius } Taken from https://developer.apple.com/videos/play/wwdc2015/414/
  • 16. Bad sharing let home = House() var temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake() Taken from https://developer.apple.com/videos/play/wwdc2015/414/
  • 17. Bad sharing Taken from https://developer.apple.com/videos/play/wwdc2015/414/ House Oven Thermostat Temperature Temperature
  • 18. –Heraclitus (Panta Rhei “Everything Flows”) “Ever-newer waters flow on those who step into the same rivers.”