Share
Embed
Transcript
21 slides
-
@attheodo “Everything we can do with classes we can do
with structs, enums and protocols” -
@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. -
@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 -
@attheodo In ObjectiveC the only way to share functionality between
classes is inheritance through Polymorphism. -
@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. -
@attheodo Whatcha say? Complex class hierarchies often lead to violations
of the DRY principle. class Dog { func bark() { print("woof") } } class GermanShepherd: Dog { } -
@attheodo Complex Class Hierarchies class Dog { … } class
GermanShepherd: Dog { func sniffDrugs() {...} } class Labrador: Dog { func sniffDrugs() { … } } class Schnautser: Dog { } code duplication -
@attheodo Complex Class Hierarchies class Dog { … func sniffDrugs()
{ … } } class GermanShepherd: Dog { … } class Labrador: Dog { … } class Schnautser: Dog { … } I don’t sniff drugs you !@#!#@$% -
@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... -
@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 -
@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! -
@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() } ... } -
@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 } } } -
@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) ... -
@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/