thinking in swift ppt
TRANSCRIPT
![Page 1: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/1.jpg)
Keith Moon - Senior iOS Developer
Thinking In Swift
MBLT.dev - MoscowNovember 2016
![Page 2: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/2.jpg)
• iOS Developer since 2010• Worked with BBC News, Hotels.com and
Travelex• Working in Swift since it’s release• Built 2 apps end to end in Swift• “Swift 3 Cookbook” to be published by Pakt
Who am I?
@keefmoon
![Page 3: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/3.jpg)
• Help users discover great local food• Make it quick and easy to order from a wide variety of
takeaways• Available on:
–Web–iOS–Android–Amazon Echo
What is Just Eat?
3
![Page 4: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/4.jpg)
• Australia• Brazil• Canada• Denmark• France• Ireland• Italy• Mexico
What is Just Eat?
Global Business
• New Zealand• Norway• Spain• Switzerland• UK
![Page 5: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/5.jpg)
• 12 iOS Developers• Organised around feature teams• Mixture of Objective-C and Swift• Regular releases• Multi-Variant Testing• Research team investigating new technology
What is Just Eat?
5
![Page 6: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/6.jpg)
What is Just Eat?
• 12 iOS Developers• Organised around feature teams• Mixture of Objective-C and Swift• Regular releases• Multi-Variant Testing• Research team investigating new technology
![Page 7: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/7.jpg)
Purpose of this talk?
• Swift is fundamentally different to Objective-C• Not just different syntax• Swift has more “tools in the toolbox”
How do we “think in Swift”?
![Page 8: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/8.jpg)
How do we “think in Swift”?
• Use constructs that more closely match the model• Write code that is hard to use wrong• Remove the need for trivial tests• Consider a Protocol orientated approach
![Page 9: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/9.jpg)
Structs
The right tool for the job
Class Objects
Enums
Protocols+ Extensions
Constrained Extensions
Tuples
![Page 10: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/10.jpg)
The right tool for the job
Enums
• Integer based
• Typedef
• …and that’s it.
typedef enum : NSUInteger { JEPaymentOptionCash, JEPaymentOptionSavedCard, JEPaymentOptionApplePay, JEPaymentOptionAccountCredit } JEPaymentOption;
- (void)payByOption:(JEPaymentOption)option { // Pay ...}
![Page 11: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/11.jpg)
The right tool for the job
Enums
• Based on any RawRepresentableenum PaymentOption: String { case cash case savedCard case applePay case accountCredit}
![Page 12: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/12.jpg)
The right tool for the job
Enums
• Based on any RawRepresentable
• … or not.
enum PaymentOption { case cash case savedCard case applePay case accountCredit}
![Page 13: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/13.jpg)
The right tool for the job
Enums
• Based on any RawRepresentable
• … or not.
• Associated Types
enum PaymentOption { case cash case savedCard(SavedCard) case applePay case accountCredit}
![Page 14: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/14.jpg)
The right tool for the job
Enums
• Based on any RawRepresentable
• … or not.
• Associated Types
• Methods
enum PaymentOption { case cash case savedCard(SavedCard) case applePay case accountCredit
func canPay(at rest: Restaurant) -> Bool { //... }}
![Page 15: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/15.jpg)
The right tool for the job
Enums
• Based on any RawRepresentable
• … or not.
• Associated Types
• Methods
• Computed variables
enum PaymentOption { case cash case savedCard(SavedCard) case applePay case accountCredit
func canPay(at rest: Restaurant) -> Bool { //... }
var isDeviceSupported: Bool { //... }}
![Page 16: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/16.jpg)
Example App
![Page 17: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/17.jpg)
Value Type Semantics
SearchFilterpostcode: BR13HP
cuisine: nil
S
![Page 18: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/18.jpg)
Value Type Semantics
SearchFilterpostcode: BR13HP
cuisine: nil
SSearchFilter
postcode: BR13HPcuisine: nil
S
![Page 19: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/19.jpg)
Value Type Semantics
SearchFilterpostcode: BR13HP
cuisine: nil
SSearchFilter
postcode: BR13HPcuisine: Chicken
S
![Page 20: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/20.jpg)
Value Type Semantics
SearchFilterpostcode: BR13HP
cuisine: nil
S
![Page 21: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/21.jpg)
Reference Type Semantics
SearchFilterpostcode: BR13HP
cuisine: nil
C
![Page 22: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/22.jpg)
Reference Type Semantics
SearchFilterpostcode: BR13HP
cuisine: nil
C
![Page 23: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/23.jpg)
Reference Type Semantics
SearchFilterpostcode: BR13HPcuisine: Chicken
C
![Page 24: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/24.jpg)
Reference Type Semantics
SearchFilterpostcode: BR13HPcuisine: Chicken
C
![Page 25: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/25.jpg)
enum Cuisine: Int {
case american case bangladeshi //... case thai case turkish init?(string: String) { guard let index = Cuisine.stringValues.index(of: string), let cuisine = Cuisine(rawValue: index) else { return nil } self = cuisine } var displayString: String { return Cuisine.stringValues[rawValue] } private static var stringValues: [String] { return ["American", "Bangladeshi", //... "Thai", "Turkish"] }}
enum Cuisine: Int, CaseCountable { case american case bangladeshi //... case thai case turkish init?(string: String) { for index in 0..<Cuisine.caseCount { if let cuisine = Cuisine(rawValue: index), cuisine.displayString == string { self = cuisine return } } return nil } var displayString: String { return String(describing: self).capitalized }}
Cuisine Enum -
NewOld
![Page 26: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/26.jpg)
restaurantService.fetchRestaurants(for: postcode) { [weak self] (fetchedRestaurants, error) in
if let fetchedRestaurants = fetchedRestaurants { self?.allRestaurants = fetchedRestaurants self?.visibleRestaurants = fetchedRestaurants self?.tableView.reloadData() } else if let error = error { // Handle Error print(error) }}
enum RestaurantResult { case success([Restaurant]) case failure(Error)}
restaurantService.fetchRestaurants(for: postcode) { [weak self] result in switch result { case .success(let fetchedRestaurants): self?.allRestaurants = fetchedRestaurants self?.visibleRestaurants = fetchedRestaurants self?.tableView.reloadData() case .failure(let error): // Handle Error print(error) }}
Networking Result -
New
Old
![Page 27: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/27.jpg)
Reference Type Semantics
Margherita PizzaC
Vegetarian PizzaC
Coca-ColaC
SpriteC
![Page 28: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/28.jpg)
Reference Type Semantics
Margherita PizzaC
Vegetarian PizzaC
Coca-ColaC
SpriteC
Discount
![Page 29: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/29.jpg)
Reference Type Semantics
Margherita PizzaC
Vegetarian PizzaC
Coca-ColaC
SpriteC
Discount
![Page 30: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/30.jpg)
Value Type Semantics
Margherita PizzaS
Vegetarian PizzaS
Vegetarian PizzaS
Coca-ColaS
Coca-ColaS
SpriteS
![Page 31: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/31.jpg)
Value Type Semantics
Margherita PizzaS
Vegetarian PizzaS
Vegetarian PizzaS
Coca-ColaS
Coca-ColaS
SpriteS
Discount
![Page 32: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/32.jpg)
Value Type Semantics
Margherita PizzaS
Vegetarian PizzaS
Vegetarian PizzaS
Coca-ColaS
Coca-ColaS
SpriteS
Discount
![Page 33: Thinking in swift ppt](https://reader034.vdocuments.us/reader034/viewer/2022042723/58f092af1a28ab57158b45d5/html5/thumbnails/33.jpg)
Summary
●Pick the appropriate Swift type for the concept being
modelled
●Consider the Type semantics
●Defining behaviours as protocols can produce expressive
code
●Avoid “Stringly” typed implementations
●Try to anticipate and prevent future developer errors