Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Practical Protocols with Associated Types

4,838 views

Published on

Presented at the Brooklyn Swift Meetup http://www.meetup.com/Brooklyn-Swift-Developers/events/232596779/

Published in: Technology
  • I just began eight weeks past and i have become four check for an entire of $4,15000...this is the best call I made in quite a while! "Much obliged to you for giving American express this unprecedented opportunity to make more cash from home. This further cash has adjusted my life in such a lot of courses in which, bestow you!".......GOOD LUCK Click this snap this connection -=-=-=-=-=-=-=-=-=-=-= http://www.cash-review.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Practical Protocols with Associated Types

  1. 1. PATs IRL @NatashaTheRobot
  2. 2. Protocol-Oriented Programming in Swift Dave Abrahams Professor of Blowing-Your-Mind
  3. 3. –- Professor of Blowing-Your-Mind "Swift is a Protocol-Oriented Programming Language"
  4. 4. Protocols with Associated Types πŸ€”
  5. 5. Protocols with Associated Types πŸ€—
  6. 6. β€’ Models β€’ Storyboards β€’ Networking
  7. 7. PATs πŸ’– Models
  8. 8. protocol HasInit { init() } class Pokemon<Power: HasInit> { func attack() -> Power { return Power() } }
  9. 9. // power types struct 🌧: HasInit { } struct 🌩: HasInit { } struct πŸ”₯: HasInit { }
  10. 10. class Pikachu: Pokemon<🌩> {} class Vaporeon: Pokemon<🌧> {} let pikachu = Pikachu() pikachu.attack() // 🌩 let vaporeon = Vaporeon() vaporeon.attack() // 🌧
  11. 11. Mixins and Traits in Swift 2.0 by Matthijs Hollemans
  12. 12. Mixins and Traits in Swift 2.0 by Matthijs Hollemans
  13. 13. protocol PowerTrait { associatedtype Power: HasInit func attack() -> Power } extension PowerTrait { func attack() -> Power { return Power() } }
  14. 14. struct Pikachu: PowerTrait { associatedtype Power = 🌩 } struct Vaporeon: PowerTrait { // 🌧 is inferred as the associated type func attack() -> 🌧 { // custom attack logic return 🌧() } }
  15. 15. PATs πŸ’– Storyboards
  16. 16. 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]
  17. 17. protocol Injectable { associatedtype Item func inject(item: Item) func assertDependencies() }
  18. 18. 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) } }
  19. 19. 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])
  20. 20. PATs πŸ’– Networking
  21. 21. struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
  22. 22. enum Result<T> { case Success(T) case Failure(ErrorType) }
  23. 23. struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
  24. 24. // 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) } } }
  25. 25. View Controller Tests?!!! 😱
  26. 26. // 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) } } }
  27. 27. // NYCFoodViewController func getFood(fromService service: FoodService) { service.getFood() { [weak self] result in // handle result } }
  28. 28. // NYCFoodViewControllerTests func testFetchFood() { viewController.getFood(fromService: FoodService()) // πŸ€” now what? }
  29. 29. struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
  30. 30. protocol Gettable { func get(completionHandler: Result<[Food]> -> Void) }
  31. 31. protocol Gettable { associatedtype T func get(completionHandler: Result<T> -> Void) }
  32. 32. struct FoodService: Gettable { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }
  33. 33. // 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) } } }
  34. 34. // NYCFoodViewControllerTests class Fake_FoodService: Gettable { var getWasCalled = false func get(completionHandler: Result<[Food]> -> Void) { getWasCalled = true completionHandler(Result.Success(food)) } }
  35. 35. // NYCFoodViewControllerTests func testFetchFood() { let fakeFoodService = Fake_FoodService() viewController.getFood(fromService: fakeFoodService) XCTAssertTrue(fakeFoodService.getWasCalled) XCTAssertEqual(viewController.dataSource.count, food.count) XCTAssertEqual(viewController.dataSource, food) }
  36. 36. T H A N K Y O U ! β€’ @NatashaTheRobot β€’ @NatashaTheNomad β€’ This Week in Swift Newsletter β€’ @tryswiftnyc πŸ₯πŸ—½πŸŽ‰ β€’ BROOKLYNSWIFT - 20% OFF β€’ Volunteers πŸ™‹

Γ—