The document discusses protocol-oriented programming in Swift. It begins by comparing protocols in Swift vs Objective-C, noting key differences like protocol inheritance, extensions, default implementations, and associated types in Swift. It then defines protocol-oriented programming as separating public interfaces from implementations using protocols that components communicate through. Examples are provided of using protocols for data types, dependency injection, testing, and real-world UIKit views. Protocol-oriented programming is said to improve reusability, extensibility, and maintainability over inheritance-based approaches.
11. protocol someProtocol {
var someInt: Int { get }
var someString: String? { get set }
func doSomething(inputA: Int) -> String?
}
12. protocol someProtocol {
associatedtype TypeA;
var someInt: Int { get }
var someString: String? { get set }
func doSomething(inputA: Int) -> String?
func doSomethingWithTypeA(typeA: TypeA)
}
13. protocol someProtocol {
associatedtype TypeA;
var someInt: Int { get }
var someString: String? { get set }
func doSomething(inputA: Int) -> String?
func doSomethingWithTypeA(typeA: TypeA)
}
protocol successorProtocol: someProtocol {
}
21. WWDC 2015 - Session 408
Protocol-Oriented Programming in Swift
“Swift is a protocol-oriented programming language.”
Dave Abrahams
22. WWDC 2015 - Session 408
Protocol-Oriented Programming in Swift
“Swift is a protocol-oriented programming language.”
Dave Abrahams
https://developer.apple.com/videos/play/wwdc2015/408/
https://developer.apple.com/videos/play/wwdc2016/419/
29. …
class Bolide: Car {
override func make() {
print("🏎 is built")
}
}
let bolide = Bolide()
let bolideFactory = CarFactory()
bolideFactory.model = bolide
bolideFactory.makeACar() // prints "🏎 is built"
Page 1 of 1
34. Implementation is a
white-box
… even using proper encapsulation
class Car {
let wheels = 4
func make() {
print("🚗 is built")
}
}
class Sedan: Car {}
38. ✦ Separation of public interface
from the implementation
✦ Software defined in components
that talk to each other using
interfaces
39. ✦ Separation of public interface
from the implementation
✦ Software defined in components
that talk to each other using
interfaces
✦ May be used in conjunction with
classes, structs and enums.
48. …
class CarFactory: VehicleFactory {
var model: Vehicle?
}
let bolide = Bolide()
let carFactory = CarFactory()
carFactory.model = bolide
carFactory.makeACar() // prints "🏎 is built"
64. import UIKit
struct Event {
let icon: UIImage?
let title: String
let date: Date
}
class EventTableViewCell: UITableViewCell {
@IBOutlet var iconView: UIImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var dateLabel: UILabel!
}
65. import UIKit
struct Event {
let icon: UIImage?
let title: String
let date: Date
}
class EventTableViewCell: UITableViewCell {
@IBOutlet var iconView: UIImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var dateLabel: UILabel!
func set(event: Event) {
iconView.image = event.icon
titleLabel.text = event.title
dateLabel.text = event.date.description
}
}
74. protocol EventViewProtocol {
var iconView: UIImageView! { get set }
var titleLabel: UILabel! { get set }
var dateLabel: UILabel! { get set }
func set(event: Event)
}
75. protocol EventViewProtocol {
var iconView: UIImageView! { get set }
var titleLabel: UILabel! { get set }
var dateLabel: UILabel! { get set }
func set(event: Event)
}
extension EventViewProtocol {
func set(event: Event) {
iconView.image = event.icon
titleLabel.text = event.title
dateLabel.text = event.date.description
}
}
76. …
class EventTableViewCell: UITableViewCell {
@IBOutlet var iconView: UIImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var dateLabel: UILabel!
}
extension EventTableViewCell: EventViewProtocol
{}
113. ✦ No mismatched strings
✦Simpler to read and maintain
✦ Notification handlers may be
classes, structs and enums
114. ✦ No mismatched strings
✦Simpler to read and maintain
✦ Notification handlers may be
classes, structs and enums
✦ … the same for notification type