Slide 1

Slide 1 text

Protocol Oriented Programming @attheodo

Slide 2

Slide 2 text

@attheodo “Swift is the world’s first protocol-oriented language”

Slide 3

Slide 3 text

@attheodo “Everything we can do with classes we can do with structs, enums and protocols”

Slide 4

Slide 4 text

@attheodo In fact... $ grep -e "^struct" stdlib.swift | wc -l 87 $ grep -e "^enum" stdlib.swift | wc -l 8 $ grep -e "^class" stdlib.swift | wc -l 4 Swift Core is based in 95 value types and 4 reference types.

Slide 5

Slide 5 text

@attheodo “Objective-C has had protocols for years…duh”

Slide 6

Slide 6 text

@attheodo Indeed, BUT... ● Actually they are what other languages call “interfaces” ● Your classes still have to inherit from a superclass (NSObject) ● … and they have to implement the protocol methods themselves

Slide 7

Slide 7 text

@attheodo In ObjectiveC the only way to share functionality between classes is inheritance through Polymorphism.

Slide 8

Slide 8 text

@attheodo In ObjectiveC the only way to share functionality between classes is inheritance through Polymorphism. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

Slide 9

Slide 9 text

@attheodo Whatcha say? Complex class hierarchies often lead to violations of the DRY principle. class Dog { func bark() { print("woof") } } class GermanShepherd: Dog { }

Slide 10

Slide 10 text

@attheodo Complex Class Hierarchies class Dog { … } class GermanShepherd: Dog { func sniffDrugs() {...} } class Labrador: Dog { func sniffDrugs() { … } } class Schnautser: Dog { } code duplication

Slide 11

Slide 11 text

@attheodo Complex Class Hierarchies class Dog { … func sniffDrugs() { … } } class GermanShepherd: Dog { … } class Labrador: Dog { … } class Schnautser: Dog { … } I don’t sniff drugs you !@#!#@$%

Slide 12

Slide 12 text

@attheodo Complex Class Hierarchies class Dog { … } class DrugsSniffingDog: Dog { func sniffDrugs() { … } } class GermanShepherd: DrugsSniffingDog { … } class Labrador: DrugsSniffingDog { … } class Schnautser: Dog { … } It’s bearable. Until we need to represent dogs who can/can’t swim...

Slide 13

Slide 13 text

@attheodo Enter Protocols ● A protocol defines a blueprint of methods, properties and other requirements that suit a particular task/functionality ● It can be adopted by a class, structure or enumeration to implement the requirements. ● A protocol can be extended to provide default implementation to some of the requirements that conforming types can take advantage of. ● Protocols are Types; Can be parameter types or return types, can be used as a type for parameters/constants, can be types in collections etc. ● Protocols can inherit one or more other protocols

Slide 14

Slide 14 text

@attheodo Enter Protocols protocol Barker { func bark() } extension Barker { func bark() { print(“woof”) } } protocol Swimmer { func swim() } extension Swimmer { func swim() { print(“plof plof”) } } class GermanShepherd: Barker { } class Labrador: Barker, Swimmer { } class Schnautser { } Composition vs Inheritance!

Slide 15

Slide 15 text

@attheodo Practical Protocol Oriented Programming

Slide 16

Slide 16 text

@attheodo Practical POP protocol ErrorAlertViewRenderer { func presentError() } extension ErrorPopoverRenderer where Self: UIViewController { func presentError() { let alert = UIAlertViewController(...) } } class MyViewController: UIViewController, ErrorAlertViewRenderer { … guard let error == nil else { presentError() } ... }

Slide 17

Slide 17 text

@attheodo Practical POP protocol Fadeable {} extension Fadeable where Self: UIView { func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0) { self.alpha = 0.0 UIView.animateWithDuration(...) { self.alpha = 1.0 } } func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0) { Self.alpha = 1.0 UIView.animateWithDuration(...) { Self.alpha = 0.0 } } }

Slide 18

Slide 18 text

@attheodo Practical POP protocol ReusableView: class {} extension ReusableView where Self: UIView { static var reuseIdentifier: String { return String(self) } } extension UITableViewCell: ReusableView {} … let cell = dequeuReusableCellWithIdentifier(MyCell.reuseIdentifier) ...

Slide 19

Slide 19 text

@attheodo Remember... Always start with a protocol.

Slide 20

Slide 20 text

@attheodo Where to go from here... ● https://developer.apple.com/library/content/documentation/Swi ft/Conceptual/Swift_Programming_Language/Protocols.html ● https://developer.apple.com/videos/play/wwdc2015/408/ ● https://realm.io/news/appbuilders-natasha-muraschev-practical- protocol-oriented-programming/

Slide 21

Slide 21 text

@attheodo Questions?