Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Emerging Best Practices in Swift
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Ash Furrow
September 14, 2015
Programming
1.5k
7
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Emerging Best Practices in Swift
Presented at iOSoho:
http://www.meetup.com/iOSoho/events/224796318/
Ash Furrow
September 14, 2015
More Decks by Ash Furrow
See All by Ash Furrow
My Slow Return to a Swift Career
ashfurrow
0
27
Migrating to React Native: A Long-Term Retrospective
ashfurrow
0
350
How Artsy Automates Team Culture
ashfurrow
0
3.4k
Building Custom TSLint Rules
ashfurrow
0
540
Circumventing Fear of the Unknown
ashfurrow
1
600
Building Better Software by Building Better Teams
ashfurrow
1
710
Building Open Source Communities
ashfurrow
0
1k
Comparative Asynchronous Programming
ashfurrow
2
9.8k
Building Compassionate Software
ashfurrow
0
600
Other Decks in Programming
See All in Programming
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
5.1k
AHC070解法紹介
eijirou
0
140
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
590
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.2k
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
110
FreeBSDでZabbixを動かす
kenkino
0
340
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
460
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
140
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
140
Intent as Code
shoppingjaws
6
1.1k
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
560
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Documentation Writing (for coders)
carmenintech
77
5.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
The Spectacular Lies of Maps
axbom
PRO
1
1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
570
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
290
A Soul's Torment
seathinner
8
3.7k
Un-Boring Meetings
codingconduct
0
430
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
690
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.