Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Practical Protocols with Associated Types

5.516 Aufrufe

Veröffentlicht am

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

Veröffentlicht in: Technologie
  • DOWNLOAD FULL. BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • 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
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

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 🙋

×