SlideShare a Scribd company logo
Keith Moon - Senior iOS Developer
Thinking In Swift
MBLT.dev - Moscow
November 2016
2
• 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
3
• 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
4
• Australia
• Brazil
• Canada
• Denmark
• France
• Ireland
• Italy
• Mexico
What is Just Eat?
Global Business
• New Zealand
• Norway
• Spain
• Switzerland
• UK
5
• 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
6
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
7
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”?
How do we “think in Swift”?
8
• 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
Structs
The right tool for the job
Class Objects
Enums
Protocols
+ Extensions
Constrained
Extensions
Tuples
Enums
• Integer based
• Typedef
• …and that’s it.
typedef enum : NSUInteger {
JEPaymentOptionCash,
JEPaymentOptionSavedCard,
JEPaymentOptionApplePay,
JEPaymentOptionAccountCredit
} JEPaymentOption;
- (void)payByOption:(JEPaymentOption)option {
// Pay ...
}
Enums
• Based on any RawRepresentable
enum PaymentOption: String {
case cash
case savedCard
case applePay
case accountCredit
}
Enums
• Based on any RawRepresentable
• … or not.
enum PaymentOption {
case cash
case savedCard
case applePay
case accountCredit
}
Enums
• Based on any RawRepresentable
• … or not.
• Associated Types
enum PaymentOption {
case cash
case savedCard(SavedCard)
case applePay
case accountCredit
}
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 {
//...
}
}
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 {
//...
}
}
Example App
16
Value Type Semantics
17
SearchFilter
postcode: BR13HP
cuisine: nil
S
Value Type Semantics
18
SearchFilter
postcode: BR13HP
cuisine: nil
S
SearchFilter
postcode: BR13HP
cuisine: nil
S
Value Type Semantics
19
SearchFilter
postcode: BR13HP
cuisine: nil
S
SearchFilter
postcode: BR13HP
cuisine: Chicken
S
Value Type Semantics
20
SearchFilter
postcode: BR13HP
cuisine: nil
S
Reference Type Semantics
21
SearchFilter
postcode: BR13HP
cuisine: nil
C
Reference Type Semantics
22
SearchFilter
postcode: BR13HP
cuisine: nil
C
Reference Type Semantics
23
SearchFilter
postcode: BR13HP
cuisine: Chicken
C
Reference Type Semantics
24
SearchFilter
postcode: BR13HP
cuisine: Chicken
C
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"]
}
}
25
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
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)
}
}
26
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
Reference Type Semantics
27
Margherita Pizza
C
Vegetarian Pizza
C
Coca-Cola
C
Sprite
C
Reference Type Semantics
28
Margherita Pizza
C
Vegetarian Pizza
C
Coca-Cola
C
Sprite
C
Reference Type Semantics
29
Margherita Pizza
C
Vegetarian Pizza
C
Coca-Cola
C
Sprite
C
Value Type Semantics
30
Margherita Pizza
S
Vegetarian Pizza
S
Vegetarian Pizza
S
Coca-Cola
S
Coca-Cola
S
Sprite
S
Value Type Semantics
31
Margherita Pizza
S
Vegetarian Pizza
S
Vegetarian Pizza
S
Coca-Cola
S
Coca-Cola
S
Sprite
S
Value Type Semantics
32
Margherita Pizza
S
Vegetarian Pizza
S
Vegetarian Pizza
S
Coca-Cola
S
Coca-Cola
S
Sprite
S
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
33
Thanks! @keefmoon
keith.moon@just-eat.com
keefmoon

More Related Content

PDF
[이모콘 2018 S/S] Swift로 코인 트레이딩 봇 만들기
PDF
Découplez votre appli en micro-APIs
PDF
A piece of sugar in your client-side development
PDF
Working with Cocoa and Objective-C
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
PPTX
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
PDF
iOS (7) Workshop
PDF
iOS NSAgora #3: Objective-C vs. Swift
[이모콘 2018 S/S] Swift로 코인 트레이딩 봇 만들기
Découplez votre appli en micro-APIs
A piece of sugar in your client-side development
Working with Cocoa and Objective-C
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
iOS (7) Workshop
iOS NSAgora #3: Objective-C vs. Swift

Viewers also liked (15)

PDF
Hello Swift Final 5/5 - Structures and Classes
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 04)
PPTX
Swift vs Objective-C
PDF
Modern Objective-C @ Pragma Night
PPT
Ios - Intorduction to view controller
PDF
iOS: Table Views
PDF
Spring MVC to iOS and the REST
PDF
Denis Lebedev, Swift
PPTX
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
PDF
Workshop iOS 2: Swift - Structures
PDF
A swift introduction to Swift
PPTX
Apple iOS
PDF
An Introduction into the design of business using business architecture
PDF
Writing Your App Swiftly
Hello Swift Final 5/5 - Structures and Classes
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 04)
Swift vs Objective-C
Modern Objective-C @ Pragma Night
Ios - Intorduction to view controller
iOS: Table Views
Spring MVC to iOS and the REST
Denis Lebedev, Swift
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
Workshop iOS 2: Swift - Structures
A swift introduction to Swift
Apple iOS
An Introduction into the design of business using business architecture
Writing Your App Swiftly
Ad

Similar to Thinking in swift ppt (20)

PDF
Diversified application testing based on a Sylius project
PDF
Swift and Kotlin Presentation
PDF
Unit testing PHP apps with PHPUnit
PDF
Deep Dive Into Swift
PDF
DSR Testing (Part 1)
PDF
Cooking your Ravioli "al dente" with Hexagonal Architecture
KEY
Tdd for BT E2E test community
PDF
Jest: Frontend Testing richtig gemacht @WebworkerNRW
KEY
Developer testing 101: Become a Testing Fanatic
PDF
Test Driven Development with JavaFX
PDF
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
PDF
10 Techniques to writing easy yet stupidly thorough unit tests.pdf
PDF
Tips for Building your First XPages Java Application
PPTX
Swift meetup22june2015
PPTX
The operation principles of PVS-Studio static code analyzer
PDF
Testing and Building Android
PDF
API first with Swagger and Scala by Slava Schmidt
PPTX
High ROI Testing in Angular.pptx
PDF
Testing the waters of iOS
PPTX
Developer’s viewpoint on swift programming language
Diversified application testing based on a Sylius project
Swift and Kotlin Presentation
Unit testing PHP apps with PHPUnit
Deep Dive Into Swift
DSR Testing (Part 1)
Cooking your Ravioli "al dente" with Hexagonal Architecture
Tdd for BT E2E test community
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Developer testing 101: Become a Testing Fanatic
Test Driven Development with JavaFX
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
10 Techniques to writing easy yet stupidly thorough unit tests.pdf
Tips for Building your First XPages Java Application
Swift meetup22june2015
The operation principles of PVS-Studio static code analyzer
Testing and Building Android
API first with Swagger and Scala by Slava Schmidt
High ROI Testing in Angular.pptx
Testing the waters of iOS
Developer’s viewpoint on swift programming language
Ad

Recently uploaded (20)

PDF
Understanding Forklifts - TECH EHS Solution
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
history of c programming in notes for students .pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Understanding Forklifts - TECH EHS Solution
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Operating system designcfffgfgggggggvggggggggg
Digital Systems & Binary Numbers (comprehensive )
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Internet Downloader Manager (IDM) Crack 6.42 Build 41
history of c programming in notes for students .pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Reimagine Home Health with the Power of Agentic AI​
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

Thinking in swift ppt

  • 1. Keith Moon - Senior iOS Developer Thinking In Swift MBLT.dev - Moscow November 2016
  • 2. 2 • 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
  • 3. 3 • 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
  • 4. 4 • Australia • Brazil • Canada • Denmark • France • Ireland • Italy • Mexico What is Just Eat? Global Business • New Zealand • Norway • Spain • Switzerland • UK
  • 5. 5 • 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
  • 6. 6 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
  • 7. 7 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”?
  • 8. How do we “think in Swift”? 8 • 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
  • 9. Structs The right tool for the job Class Objects Enums Protocols + Extensions Constrained Extensions Tuples
  • 10. Enums • Integer based • Typedef • …and that’s it. typedef enum : NSUInteger { JEPaymentOptionCash, JEPaymentOptionSavedCard, JEPaymentOptionApplePay, JEPaymentOptionAccountCredit } JEPaymentOption; - (void)payByOption:(JEPaymentOption)option { // Pay ... }
  • 11. Enums • Based on any RawRepresentable enum PaymentOption: String { case cash case savedCard case applePay case accountCredit }
  • 12. Enums • Based on any RawRepresentable • … or not. enum PaymentOption { case cash case savedCard case applePay case accountCredit }
  • 13. Enums • Based on any RawRepresentable • … or not. • Associated Types enum PaymentOption { case cash case savedCard(SavedCard) case applePay case accountCredit }
  • 14. 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 { //... } }
  • 15. 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 { //... } }
  • 18. Value Type Semantics 18 SearchFilter postcode: BR13HP cuisine: nil S SearchFilter postcode: BR13HP cuisine: nil S
  • 19. Value Type Semantics 19 SearchFilter postcode: BR13HP cuisine: nil S SearchFilter postcode: BR13HP cuisine: Chicken S
  • 25. 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"] } } 25 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
  • 26. 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) } } 26 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
  • 27. Reference Type Semantics 27 Margherita Pizza C Vegetarian Pizza C Coca-Cola C Sprite C
  • 28. Reference Type Semantics 28 Margherita Pizza C Vegetarian Pizza C Coca-Cola C Sprite C
  • 29. Reference Type Semantics 29 Margherita Pizza C Vegetarian Pizza C Coca-Cola C Sprite C
  • 30. Value Type Semantics 30 Margherita Pizza S Vegetarian Pizza S Vegetarian Pizza S Coca-Cola S Coca-Cola S Sprite S
  • 31. Value Type Semantics 31 Margherita Pizza S Vegetarian Pizza S Vegetarian Pizza S Coca-Cola S Coca-Cola S Sprite S
  • 32. Value Type Semantics 32 Margherita Pizza S Vegetarian Pizza S Vegetarian Pizza S Coca-Cola S Coca-Cola S Sprite S
  • 33. 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 33