Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Emerging Best Practices in Swift
Search
Ash Furrow
September 14, 2015
Programming
7
1.4k
Emerging Best Practices in Swift
Presented at iOSoho:
http://www.meetup.com/iOSoho/events/224796318/
Ash Furrow
September 14, 2015
Tweet
Share
More Decks by Ash Furrow
See All by Ash Furrow
Migrating to React Native: A Long-Term Retrospective
ashfurrow
0
260
How Artsy Automates Team Culture
ashfurrow
0
3.2k
Building Custom TSLint Rules
ashfurrow
0
440
Circumventing Fear of the Unknown
ashfurrow
1
540
Building Better Software by Building Better Teams
ashfurrow
1
590
Building Open Source Communities
ashfurrow
0
880
Comparative Asynchronous Programming
ashfurrow
2
9.6k
Building Compassionate Software
ashfurrow
0
470
Swift, Briskly
ashfurrow
0
150
Other Decks in Programming
See All in Programming
Go コードベースの構成と AI コンテキスト定義
andpad
0
150
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
520
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
330
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
460
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
3.3k
チームをチームにするEM
hitode909
0
430
CSC307 Lecture 01
javiergs
PRO
0
650
はじめてのカスタムエージェント【GitHub Copilot Agent Mode編】
satoshi256kbyte
0
140
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.4k
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
0
190
ローカルLLMを⽤いてコード補完を⾏う VSCode拡張機能を作ってみた
nearme_tech
PRO
0
240
これならできる!個人開発のすゝめ
tinykitten
PRO
0
140
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
730
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
61
51k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
360
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Mobile First: as difficult as doing things right
swwweet
225
10k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
1
880
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
115
100k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
76
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
27
Transcript
Emerging Best Practices in Swift Ash Furrow
None
I was Afraid That we'd just write Objective-C in Swift
syntax.
Everything turned out Fine
Today, we're exploring best practices in Swift.
We've been here before. Swift 2 is significantly different. Always
be learning.
Let's Go
Those who don't know history are doomed to repeat it.
— Lots of people.
Wrong.
Those who don't know the past can't make informed decisions
about the present. — Me
iOS 5 or Earlier? Let's see a show of hands.
Before Object Literals NSArray *array = [NSArray arrayWithObjects: @"This", @"is",
@"so", @"tedious", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Who would do this?", @"Not me", nil]; NSNumber *number = [NSNumber numberWithInt: 401];
Before Object Literals and ARC NSArray *array = [[NSArray arrayWithObjects:
@"This", @"is", @"so", @"tedious", nil] retain]; NSDictionary *dictionary = [[NSDictionary dictionaryWithObjectsAndKeys: @"Who would do this?", @"Not me", nil] retain]; NSNumber *number = [[NSNumber numberWithInt: 401] retain];
!
After Object Literals NSArray *array = @[ @"This", @"is", @"much",
@"better" ]; NSDictionary *dictionary = @{ @"Who likes this?": @"Me!" }; NSNumber *number = @(401);
Adopted immediately. Clearly better. Became a best practice.
Blocks — iOS 4 introduced blocks and GCD. — Adopted...
eventually. — Required new ideas. — Became a best practice.
Blocks — iOS 4 introduced blocks and GCD. — Adopted...
eventually. — Required new ideas. — Became a best practice. — Blocks now enable other best practices.
Swift 2
Swift 2 — Lots of new syntax. — New syntax
lets us do cool new things. — Like blocks, syntax is only a tool.
Swift 2 — guard — defer — throws — etc...
Should I use guard?
What can I do with guard?
Examples
If Overload if let thing = optionalThing { if thing.shouldDoThing
{ if let otherThing = thing.otherThing { doStuffWithThing(otherThing) } } }
where to the rescue if let thing = optionalThing, let
otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) }
where to the rescue if let thing = optionalThing, let
otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) } if...where isn't cool. You know what's cool?
where to the rescue if let thing = optionalThing, let
otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) } if...where isn't cool. You know what's cool? ... Neither do I. Let's look together!
Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String]
{ var mutableArray = [String]() for string in parameter { if string.hasPrefix(prefix) { mutableArray.append(string) } } return mutableArray }
Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String]
{ var mutableArray = [String]() for string in parameter { if string.hasPrefix(prefix) { mutableArray.append(string) } } return mutableArray } That's silly.
Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String]
{ return parameter.filter { $0.hasPrefix(prefix) } }
Extract associated values 1. Use Swift enums. 2. Attach associated
values. 3. Extract using case.
Extract associated values enum Result<T> { case Success(T) case Failure(reason:
String) } ... switch doThing() { case .Success: print("!") case .Failure(let reason): print("Oops: \(reason)") }
Extract associated values if case .Success = doThing() { print("!")
}
That's all just syntax.
Protocol-Oriented Programming
Just go watch the WWDC video.
Syntax itself is not a best practice. The patterns enabled
by syntax are what really matter. We need to discover them.
Learning !
Learning shouldn't just happen during the Xcode betas.
Learning is a constant activity, a state of mind.
Learning — Look for code smells. — Ask yourself how
you'd solve something differently. — Pick a Swift feature, ask "what could I do with this?" — Be comfortable throwing code away.
What about other communities? I bet they have good ideas,
too...
You should write a Blog
Wrap-up
We have a history of being awesome, let's keep it
up. Re-evaluate solutions to familiar problems. Always be learning. Also, write a blog.
Make better mistakes tomorrow.