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
280
How Artsy Automates Team Culture
ashfurrow
0
3.3k
Building Custom TSLint Rules
ashfurrow
0
460
Circumventing Fear of the Unknown
ashfurrow
1
560
Building Better Software by Building Better Teams
ashfurrow
1
620
Building Open Source Communities
ashfurrow
0
920
Comparative Asynchronous Programming
ashfurrow
2
9.7k
Building Compassionate Software
ashfurrow
0
500
Swift, Briskly
ashfurrow
0
170
Other Decks in Programming
See All in Programming
Agent Skills Workshop - AIへの頼み方を仕組み化する
gotalab555
15
8.5k
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
560
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
420
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
380
Codex の「自走力」を高める
yorifuji
0
1.2k
ふつうのRubyist、ちいさなデバイス、大きな一年 / Ordinary Rubyists, Tiny Devices, Big Year
chobishiba
1
430
AI時代のシステム設計:ドメインモデルで変更しやすさを守る設計戦略
masuda220
PRO
5
840
AI時代のソフトウェア開発でも「人が仕様を書く」から始めよう-医療IT現場での実践とこれから
koukimiura
0
140
nilとは何か 〜interfaceの構造とnil!=nilから理解する〜
kuro_kurorrr
3
1.9k
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
420
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
960
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
740
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
310
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
390
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.8k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
How GitHub (no longer) Works
holman
316
140k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
330
Designing Experiences People Love
moore
143
24k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
Facilitating Awesome Meetings
lara
57
6.8k
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.