Practical Protocols with Associated Types
-
Upload
natasha-murashev -
Category
Technology
-
view
2.098 -
download
0
Transcript of Practical Protocols with Associated Types
![Page 1: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/1.jpg)
PATs IRL@NatashaTheRobot
![Page 2: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/2.jpg)
Protocol-Oriented Programming in Swift Dave Abrahams Professor of Blowing-Your-Mind
![Page 3: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/3.jpg)
![Page 4: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/4.jpg)
–- Professor of Blowing-Your-Mind
"Swift is a Protocol-Oriented Programming Language"
![Page 5: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/5.jpg)
Protocols with Associated Types 🤔
![Page 6: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/6.jpg)
![Page 7: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/7.jpg)
![Page 8: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/8.jpg)
![Page 9: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/9.jpg)
![Page 10: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/10.jpg)
![Page 11: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/11.jpg)
Protocols with Associated Types 🤗
![Page 12: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/12.jpg)
• Models
• Storyboards
• Networking
![Page 13: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/13.jpg)
PATs 💖 Models
![Page 14: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/14.jpg)
![Page 15: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/15.jpg)
![Page 16: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/16.jpg)
protocol HasInit { init() }
class Pokemon<Power: HasInit> { func attack() -> Power { return Power() } }
![Page 17: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/17.jpg)
// power types struct 🌧: HasInit { } struct 🌩: HasInit { } struct 🔥: HasInit { }
![Page 18: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/18.jpg)
class Pikachu: Pokemon<🌩> {} class Vaporeon: Pokemon<🌧> {}
let pikachu = Pikachu() pikachu.attack() // 🌩
let vaporeon = Vaporeon() vaporeon.attack() // 🌧
![Page 19: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/19.jpg)
![Page 20: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/20.jpg)
Mixins and Traits in Swift 2.0 by Matthijs Hollemans
![Page 21: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/21.jpg)
Mixins and Traits in Swift 2.0 by Matthijs Hollemans
![Page 22: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/22.jpg)
protocol PowerTrait { associatedtype Power: HasInit func attack() -> Power }
extension PowerTrait { func attack() -> Power { return Power() } }
![Page 23: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/23.jpg)
struct Pikachu: PowerTrait { associatedtype Power = 🌩 }
struct Vaporeon: PowerTrait { // 🌧 is inferred as the associated type func attack() -> 🌧 { // custom attack logic return 🌧() } }
![Page 24: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/24.jpg)
![Page 25: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/25.jpg)
PATs 💖 Storyboards
![Page 26: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/26.jpg)
![Page 27: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/27.jpg)
![Page 28: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/28.jpg)
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) { switch segueIdentifier(forSegue: segue) { case .StatueOfLibertyAdventure: let statueOfLibertyVC = segue.destinationViewController as? StatueOfLiberityViewController statueOfLibertyVC?.user = user case .EmpireStateAdventure: let empireStateVC = segue.destinationViewController as? EmpireStateViewController
empireStateVC?.friends = [friend1, friend2]
![Page 29: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/29.jpg)
protocol Injectable { associatedtype Item func inject(item: Item) func assertDependencies() }
![Page 30: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/30.jpg)
class AdventureDetailViewController: UIViewController, Injectable {
private var user: User! override func viewDidLoad() { super.viewDidLoad()
assertDependencies() // Do any additional setup after loading the view. } func inject(item: User) { user = item } func assertDependencies() { assert(user != nil) }
}
![Page 31: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/31.jpg)
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) { switch segueIdentifier(forSegue: segue) { case .StatueOfLibertyAdventure: let statueOfLibertyVC = segue.destinationViewController as? StatueOfLiberityViewController statueOfLibertyVC?.inject(item: user) case .EmpireStateAdventure: let empireStateVC = segue.destinationViewController as? EmpireStateViewController
empireStateVC?.inject(item: [friend1, friend2])
![Page 32: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/32.jpg)
![Page 33: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/33.jpg)
PATs 💖 Networking
![Page 34: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/34.jpg)
![Page 35: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/35.jpg)
struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
![Page 36: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/36.jpg)
enum Result<T> { case Success(T) case Failure(ErrorType) }
![Page 37: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/37.jpg)
struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
![Page 38: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/38.jpg)
// NYCFoodViewController
var dataSource = [Food]() { didSet { tableView.reloadData() } }
override func viewDidLoad() { super.viewDidLoad() getFood() }
private func getFood() { FoodService().get() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }
![Page 39: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/39.jpg)
View Controller Tests?!!! 😱
![Page 40: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/40.jpg)
// NYCFoodViewController
private func getFood() { FoodService().get() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }
![Page 41: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/41.jpg)
// NYCFoodViewController
func getFood(fromService service: FoodService) {
service.getFood() { [weak self] result in // handle result } }
![Page 42: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/42.jpg)
// NYCFoodViewControllerTests
func testFetchFood() { viewController.getFood(fromService: FoodService()) // 🤔 now what? }
![Page 43: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/43.jpg)
struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
![Page 44: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/44.jpg)
protocol Gettable { func get(completionHandler: Result<[Food]> -> Void) }
![Page 45: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/45.jpg)
protocol Gettable { associatedtype T func get(completionHandler: Result<T> -> Void) }
![Page 46: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/46.jpg)
struct FoodService: Gettable { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
![Page 47: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/47.jpg)
// NYCFoodViewController
override func viewDidLoad() { super.viewDidLoad() getFood(fromService: FoodService()) }
func getFood<S: Gettable where S.T == [Food]>(fromService service: S) { service.get() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }
![Page 48: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/48.jpg)
// NYCFoodViewControllerTests
class Fake_FoodService: Gettable { var getWasCalled = false func get(completionHandler: Result<[Food]> -> Void) { getWasCalled = true completionHandler(Result.Success(food)) } }
![Page 49: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/49.jpg)
// NYCFoodViewControllerTests
func testFetchFood() { let fakeFoodService = Fake_FoodService() viewController.getFood(fromService: fakeFoodService) XCTAssertTrue(fakeFoodService.getWasCalled) XCTAssertEqual(viewController.dataSource.count, food.count) XCTAssertEqual(viewController.dataSource, food) }
![Page 50: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/50.jpg)
![Page 51: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/51.jpg)
![Page 52: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/52.jpg)
![Page 53: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/53.jpg)
![Page 54: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/54.jpg)
![Page 55: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/55.jpg)
![Page 56: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/56.jpg)
![Page 57: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/57.jpg)
![Page 58: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/58.jpg)
![Page 59: Practical Protocols with Associated Types](https://reader034.fdocuments.net/reader034/viewer/2022051706/5873c1a11a28abbc788b68b7/html5/thumbnails/59.jpg)
T H A N K Y O U !
• @NatashaTheRobot
• @NatashaTheNomad
• This Week in Swift Newsletter
• @tryswiftnyc 🐥🗽🎉
• BROOKLYNSWIFT - 20% OFF
• Volunteers 🙋