SlideShare a Scribd company logo
Swift Thinking
@NatashaTheRobot
Back to December
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
println("Hello, World!")
Swift Thinking
Swift Thinking
Swift Thinking
Swift Thinking
—Optionals?!
—Value Types
—Higher-order functions
Optionals?!
"~40% of bugs shipped to
customers in the last
three years would have
been caught immediately
by using Swift" - Sunset
Lake Software
Swift Thinking
enum Optional<T> {
case Some(T)
case None
}
let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
let possibleAlbumFrom2011: String? = tSwiftAlbums[2011]
let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
Swift Thinking
let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
if possibleAlbumFrom2014 == .None {
println("Taylor Swift had no albums in 2014")
} else {
let albumFrom2014 = possibleAlbumFrom2014!
println("Taylor Swift's 2014 album was (albumFrom2014)")
}
let tSwiftAlbums = [
2014 : "1989",
2012: "Red",
2010: "Speak Now",
2008: "Fearless",
2006: "Taylor Swift"]
if let albumFor2014 = tSwiftAlbums[2014] {
println("Taylor Swift's 2014 album was (albumFor2014)")
} else {
println("Taylor Swift had no albums in 2014")
}
Swift Thinking
Value Types
—structs
—enums
—(tuples)
class Album {
let title: String
let artist: String
var copiesSold: Int
init(title: String, artist: String, copiesSold: Int) {
self.title = title
self.artist = artist
self.copiesSold = copiesSold
}
}
let tSwift1989 = Album(title: "1989",
artist: "Taylor Swift",
copiesSold: 4505000)
func another1000Sales(forAlbum album: Album) {
album.copiesSold += 1000
}
another1000Sales(forAlbum: tSwift1989)
tSwift1989.copiesSold // 4,506,000
struct Album {
let title: String
let artist: String
var copiesSold: Int
}
let tSwift1989 = Album(title: "1989",
artist: "Taylor Swift",
copiesSold: 4505000)
func another1000Sales(var forAlbum album: Album) {
album.copiesSold += 1000
album.copiesSold // 4,506,000
}
another1000Sales(forAlbum: tSwift1989)
tSwift1989.copiesSold // 4,505,000
Use a value type when:
—Comparing instance data with == makes sense
—You want copies to have independent state
—The data will be used in code across multiple
threads
Swift Blog: Value and Reference Types
Use a reference type (e.g. use a class) when:
—Comparing instance identity with == makes sense
—You want to create shared, mutable state
Swift Blog: Value and Reference Types
"Almost all types in Swift are value
types, including arrays,
dictionaries, numbers, booleans,
tuples, and enums. Classes are the
exception rather than the rule." -
Functional Swift Book
$ grep -e "^struct " swift.md | wc -l
81
$ grep -e "^enum " swift.md | wc -l
8
$ grep -e "^class " swift.md | wc -l
3
Higher-order Functions
a higher-order function is a function that does at
least one of the following:
—takes one or more functions as an input
—outputs a function
- Wikipedia
Array
—map
—reduce
—filter
—flatMap
struct Song {
let title: String
let album: String
}
let tSwiftSongs = [
Song(title: "Blank Space", album: "1989"),
Song(title: "All You Had to Do Was Stay", album: "Red"),
Song(title: "Back to December", album: "Speak Now"),
Song(title: "All You Had to Do Was Stay", album: "1989"),
Song(title: "Begin Again", album: "Red"),
Song(title: "Clean", album: "1989"),
Song(title: "Love Story", album: "Fearless"),
Song(title: "Shake It Off", album: "1989"),
Song(title: "Bad Blood", album: "1989")
]
struct tSwift1989Album {
let title = "1989"
var songs = [Song]()
}
class tSwift1989Album {
let title = "1989"
var songs = [Song]()
func add1989Songs() {
for song in tSwiftSongs {
if song.album == "1989" {
songs.append(song)
}
}
}
}
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
// MUCH FURTHER DOWN
album.add1989Songs()
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
// MUCH FURTHER DOWN
album.add1989Songs()
album.songs.count // 10
let album = tSwift1989Album()
album.add1989Songs()
album.songs.count // 5
album.add1989Songs()
album.songs.count // 10
album.add1989Songs()
album.songs.count // 15
album.add1989Songs()
album.songs.count // 20
/// Return an `Array` containing the elements `x` of `self` for which
/// `includeElement(x)` is `true`
func filter(includeElement: (T) -> Bool) -> [T]
class tSwift1989Album {
let title = "1989"
var songs = [Song]()
func add1989SongsWithFilter() {
songs = tSwiftSongs.filter({ song in song.album == "1989"})
}
}
songs = tSwiftSongs.filter({ song in song.album == "1989"})
songs = tSwiftSongs.filter({ song in song.album == "1989"})
songs = tSwiftSongs.filter({ $0.album == "1989"})
songs = tSwiftSongs.filter({ song in song.album == "1989"})
songs = tSwiftSongs.filter({ $0.album == "1989"})
songs = tSwiftSongs.filter { $0.album == "1989"}
let album = tSwift1989Album()
album.add1989SongsWithFilter()
album.songs.count // 5
album.add1989SongsWithFilter()
album.songs.count // 5
album.add1989SongsWithFilter()
album.songs.count // 5
album.add1989SongsWithFilter()
album.songs.count // 5
album.add1989SongsWithFilter()
album.songs.count // 5
Swift Thinking
Swift Thinking
—Optionals?!
—Value Types
—Higher-order functions
Questions?!
@NatashaTheRobot

More Related Content

Viewers also liked (18)

PPT
Social media strategies slideshare version
Kella Price
 
PPT
Proyecto final jonathan lara reyes
JONLR
 
PPT
Three Things...
OH TEIK BIN
 
PDF
BAAL-SIG-LLT-2016-Programme-Booklet_FINAL
Helen Lee
 
PPTX
Control panel
Blackboard Swansea
 
PPTX
State of open research data open con
Amye Kenall
 
PPS
ابي لا تكن سبب هلاكي
abuomar75
 
DOCX
Qué tipo de compuesto es el agua
jose hugo ruvalcaba vadillo
 
PDF
Технологии виртуальной и дополненной реальности в маркетинге и рекламе
The International Association of Marketing Initiatives (IAMI)
 
PDF
Welding process
Darawan Wahid
 
PDF
Workshop #14: Behaviour, government policy and me: applying behavioural insig...
ux singapore
 
PDF
Multi-Omics Bioinformatics across Application Domains
Christoph Steinbeck
 
PPTX
Selling Account-Based Marketing (ABM) Internally To Your Organization
Heinz Marketing Inc
 
PDF
How to Get Into Big Data: Women in Tech Philly
Vicki Boykis
 
PDF
What Is Value Added For Security Guard Company Clients?
OfficerReports.com
 
PPTX
100+ Best Quotes from Cannes Lions 2016
Michael Boamah
 
PDF
LinkedIn Ads Playbook
Francesca Lazzini
 
PPT
Cibeles Madrid Fashion Week 2009
Compulsiva Accesorios
 
Social media strategies slideshare version
Kella Price
 
Proyecto final jonathan lara reyes
JONLR
 
Three Things...
OH TEIK BIN
 
BAAL-SIG-LLT-2016-Programme-Booklet_FINAL
Helen Lee
 
Control panel
Blackboard Swansea
 
State of open research data open con
Amye Kenall
 
ابي لا تكن سبب هلاكي
abuomar75
 
Qué tipo de compuesto es el agua
jose hugo ruvalcaba vadillo
 
Технологии виртуальной и дополненной реальности в маркетинге и рекламе
The International Association of Marketing Initiatives (IAMI)
 
Welding process
Darawan Wahid
 
Workshop #14: Behaviour, government policy and me: applying behavioural insig...
ux singapore
 
Multi-Omics Bioinformatics across Application Domains
Christoph Steinbeck
 
Selling Account-Based Marketing (ABM) Internally To Your Organization
Heinz Marketing Inc
 
How to Get Into Big Data: Women in Tech Philly
Vicki Boykis
 
What Is Value Added For Security Guard Company Clients?
OfficerReports.com
 
100+ Best Quotes from Cannes Lions 2016
Michael Boamah
 
LinkedIn Ads Playbook
Francesca Lazzini
 
Cibeles Madrid Fashion Week 2009
Compulsiva Accesorios
 

Similar to Swift Thinking (11)

PDF
Many of us have large digital music collections that are not always .pdf
fazanmobiles
 
PDF
Intro toswift1
Jordan Morgan
 
PPT
Introduction to regular expressions
Ben Brumfield
 
PDF
What Are For Expressions in Scala?
BoldRadius Solutions
 
PDF
Can someone please help me implement the addSong function .pdf
akshpatil4
 
PDF
I need help writing the methods for the song and playList class in J.pdf
arihanthtextiles
 
PPT
Textpad and Regular Expressions
OCSI
 
PDF
Fun never stops. introduction to haskell programming language
Pawel Szulc
 
PDF
Programming in Vinyl (BayHac 2014)
jonsterling
 
PPTX
Bioinformatica p2-p3-introduction
Prof. Wim Van Criekinge
 
PDF
Snakes and ladders
Rubén Berenguel
 
Many of us have large digital music collections that are not always .pdf
fazanmobiles
 
Intro toswift1
Jordan Morgan
 
Introduction to regular expressions
Ben Brumfield
 
What Are For Expressions in Scala?
BoldRadius Solutions
 
Can someone please help me implement the addSong function .pdf
akshpatil4
 
I need help writing the methods for the song and playList class in J.pdf
arihanthtextiles
 
Textpad and Regular Expressions
OCSI
 
Fun never stops. introduction to haskell programming language
Pawel Szulc
 
Programming in Vinyl (BayHac 2014)
jonsterling
 
Bioinformatica p2-p3-introduction
Prof. Wim Van Criekinge
 
Snakes and ladders
Rubén Berenguel
 
Ad

More from Natasha Murashev (20)

PDF
Digital Nomad: The New Normal
Natasha Murashev
 
PDF
Swift Delhi: Practical POP
Natasha Murashev
 
PDF
Build Features Not Apps
Natasha Murashev
 
PDF
Build Features Not Apps
Natasha Murashev
 
PDF
Practical Protocols with Associated Types
Natasha Murashev
 
PDF
The Secret Life of a Digital Nomad
Natasha Murashev
 
PDF
How to Win on the Apple Watch
Natasha Murashev
 
PDF
Hello watchOS2
Natasha Murashev
 
PDF
Practical Protocol-Oriented-Programming
Natasha Murashev
 
PDF
Protocol Oriented MVVM - Auckland iOS Meetup
Natasha Murashev
 
PDF
Protocol-Oriented MVVM (extended edition)
Natasha Murashev
 
PDF
Protocol-Oriented MVVM
Natasha Murashev
 
PDF
The Swift Architect
Natasha Murashev
 
PDF
The Zen Guide to WatchOS 2
Natasha Murashev
 
PDF
HealthKit Deep Dive
Natasha Murashev
 
PDF
Using Parse in Hackathons
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
Unleash the Power of Playgrounds
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
Digital Nomad: The New Normal
Natasha Murashev
 
Swift Delhi: Practical POP
Natasha Murashev
 
Build Features Not Apps
Natasha Murashev
 
Build Features Not Apps
Natasha Murashev
 
Practical Protocols with Associated Types
Natasha Murashev
 
The Secret Life of a Digital Nomad
Natasha Murashev
 
How to Win on the Apple Watch
Natasha Murashev
 
Hello watchOS2
Natasha Murashev
 
Practical Protocol-Oriented-Programming
Natasha Murashev
 
Protocol Oriented MVVM - Auckland iOS Meetup
Natasha Murashev
 
Protocol-Oriented MVVM (extended edition)
Natasha Murashev
 
Protocol-Oriented MVVM
Natasha Murashev
 
The Swift Architect
Natasha Murashev
 
The Zen Guide to WatchOS 2
Natasha Murashev
 
HealthKit Deep Dive
Natasha Murashev
 
Using Parse in Hackathons
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Unleash the Power of Playgrounds
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Ad

Recently uploaded (20)

PPTX
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
AI Prompts Cheat Code prompt engineering
Avijit Kumar Roy
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
AI Prompts Cheat Code prompt engineering
Avijit Kumar Roy
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 

Swift Thinking

  • 2. Back to December #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0; }
  • 10. "~40% of bugs shipped to customers in the last three years would have been caught immediately by using Swift" - Sunset Lake Software
  • 12. enum Optional<T> { case Some(T) case None }
  • 13. let tSwiftAlbums = [ 2014 : "1989", 2012: "Red", 2010: "Speak Now", 2008: "Fearless", 2006: "Taylor Swift"] let possibleAlbumFrom2011: String? = tSwiftAlbums[2011] let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
  • 15. let tSwiftAlbums = [ 2014 : "1989", 2012: "Red", 2010: "Speak Now", 2008: "Fearless", 2006: "Taylor Swift"] let possibleAlbumFrom2014: String? = tSwiftAlbums[2014] if possibleAlbumFrom2014 == .None { println("Taylor Swift had no albums in 2014") } else { let albumFrom2014 = possibleAlbumFrom2014! println("Taylor Swift's 2014 album was (albumFrom2014)") }
  • 16. let tSwiftAlbums = [ 2014 : "1989", 2012: "Red", 2010: "Speak Now", 2008: "Fearless", 2006: "Taylor Swift"] if let albumFor2014 = tSwiftAlbums[2014] { println("Taylor Swift's 2014 album was (albumFor2014)") } else { println("Taylor Swift had no albums in 2014") }
  • 20. class Album { let title: String let artist: String var copiesSold: Int init(title: String, artist: String, copiesSold: Int) { self.title = title self.artist = artist self.copiesSold = copiesSold } }
  • 21. let tSwift1989 = Album(title: "1989", artist: "Taylor Swift", copiesSold: 4505000) func another1000Sales(forAlbum album: Album) { album.copiesSold += 1000 } another1000Sales(forAlbum: tSwift1989) tSwift1989.copiesSold // 4,506,000
  • 22. struct Album { let title: String let artist: String var copiesSold: Int }
  • 23. let tSwift1989 = Album(title: "1989", artist: "Taylor Swift", copiesSold: 4505000) func another1000Sales(var forAlbum album: Album) { album.copiesSold += 1000 album.copiesSold // 4,506,000 } another1000Sales(forAlbum: tSwift1989) tSwift1989.copiesSold // 4,505,000
  • 24. Use a value type when: —Comparing instance data with == makes sense —You want copies to have independent state —The data will be used in code across multiple threads Swift Blog: Value and Reference Types
  • 25. Use a reference type (e.g. use a class) when: —Comparing instance identity with == makes sense —You want to create shared, mutable state Swift Blog: Value and Reference Types
  • 26. "Almost all types in Swift are value types, including arrays, dictionaries, numbers, booleans, tuples, and enums. Classes are the exception rather than the rule." - Functional Swift Book
  • 27. $ grep -e "^struct " swift.md | wc -l 81 $ grep -e "^enum " swift.md | wc -l 8 $ grep -e "^class " swift.md | wc -l 3
  • 29. a higher-order function is a function that does at least one of the following: —takes one or more functions as an input —outputs a function - Wikipedia
  • 31. struct Song { let title: String let album: String } let tSwiftSongs = [ Song(title: "Blank Space", album: "1989"), Song(title: "All You Had to Do Was Stay", album: "Red"), Song(title: "Back to December", album: "Speak Now"), Song(title: "All You Had to Do Was Stay", album: "1989"), Song(title: "Begin Again", album: "Red"), Song(title: "Clean", album: "1989"), Song(title: "Love Story", album: "Fearless"), Song(title: "Shake It Off", album: "1989"), Song(title: "Bad Blood", album: "1989") ]
  • 32. struct tSwift1989Album { let title = "1989" var songs = [Song]() }
  • 33. class tSwift1989Album { let title = "1989" var songs = [Song]() func add1989Songs() { for song in tSwiftSongs { if song.album == "1989" { songs.append(song) } } } }
  • 34. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5
  • 35. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5 // MUCH FURTHER DOWN album.add1989Songs()
  • 36. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5 // MUCH FURTHER DOWN album.add1989Songs() album.songs.count // 10
  • 37. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5 album.add1989Songs() album.songs.count // 10 album.add1989Songs() album.songs.count // 15 album.add1989Songs() album.songs.count // 20
  • 38. /// Return an `Array` containing the elements `x` of `self` for which /// `includeElement(x)` is `true` func filter(includeElement: (T) -> Bool) -> [T]
  • 39. class tSwift1989Album { let title = "1989" var songs = [Song]() func add1989SongsWithFilter() { songs = tSwiftSongs.filter({ song in song.album == "1989"}) } }
  • 40. songs = tSwiftSongs.filter({ song in song.album == "1989"})
  • 41. songs = tSwiftSongs.filter({ song in song.album == "1989"}) songs = tSwiftSongs.filter({ $0.album == "1989"})
  • 42. songs = tSwiftSongs.filter({ song in song.album == "1989"}) songs = tSwiftSongs.filter({ $0.album == "1989"}) songs = tSwiftSongs.filter { $0.album == "1989"}
  • 43. let album = tSwift1989Album() album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5