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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
40
Migrating to React Native: A Long-Term Retrospective
ashfurrow
0
370
How Artsy Automates Team Culture
ashfurrow
0
3.5k
Building Custom TSLint Rules
ashfurrow
0
550
Circumventing Fear of the Unknown
ashfurrow
1
630
Building Better Software by Building Better Teams
ashfurrow
1
740
Building Open Source Communities
ashfurrow
0
1.1k
Comparative Asynchronous Programming
ashfurrow
2
9.9k
Building Compassionate Software
ashfurrow
0
630
Other Decks in Programming
See All in Programming
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
300
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
310
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
5.8k
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
1.9k
AI駆動開発4年目の現在地(『あなたはFDEになるべきか?』イベント資料)
kumaiu
0
520
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
2k
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
220
Seven New Principles of Software Engineering
yegor256
0
470
Goコンパイラを作ってGoをもっと理解する / Build a Go compiler to better understand Go
ikura_hamu
0
130
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
3
1.5k
オーケストレーション記述言語 Katari
yukikurage
0
240
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
580
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
600
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
590
Product Roadmaps are Hard
iamctodd
55
13k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Visualization
eitanlees
153
17k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
The Cost Of JavaScript in 2023
addyosmani
56
10k
Practical Orchestrator
shlominoach
192
12k
GraphQLとの向き合い方2022年版
quramy
50
15k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.3k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
980
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.