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
Discovering Native Swift Patterns
Search
Nick O'Neill
June 09, 2016
Programming
14k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Discovering Native Swift Patterns
Nick O'Neill
June 09, 2016
Other Decks in Programming
See All in Programming
はてなアカウント基盤 State of the Union
cockscomb
1
900
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
970
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
170
Contextとはなにか
chiroruxx
1
380
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
230
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
280
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.8k
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
230
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
750
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
860
Featured
See All Featured
How to build a perfect <img>
jonoalderson
1
5.7k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
440
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.9k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Unsuck your backbone
ammeep
672
58k
A Tale of Four Properties
chriscoyier
163
24k
4 Signs Your Business is Dying
shpigford
187
22k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
310
How STYLIGHT went responsive
nonsquared
100
6.2k
Transcript
Discovering Na.ve Swi1 Pa3erns Developing clear, consise code Nick O'Neill
• @nickoneill Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
That Thing in Swi+ let's think swi$y Discovering Na.ve Swi1
Pa3erns • June 9th, 2016
How can I express what I'm doing more clearly? Discovering
Na.ve Swi1 Pa3erns • June 9th, 2016
Sta$c table cells Discovering Na.ve Swi1 Pa3erns • June 9th,
2016
the objec)ve-c way if (indexPath.section == 0) { if (indexPath.row
== 0) { cell.textLabel.text = @"Twitter" } else if (indexPath.row == 1) { cell.textLabel.text = @"Blog" } else { cell.textLabel.text = @"Contact Us" } } else { if (indexPath.row == 0) { cell.textLabel.text = @“@nickoneill" } else if (indexPath.row == 1) { cell.textLabel.text = @“@objctoswift" } else { cell.textLabel.text = @“@whyareyousodumb" } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
a be%er? swi+ way let shortPath = (indexPath.section, indexPath.row) switch
shortPath { case (0, 0): cell.textLabel.text = "Twitter" case (0, 1): cell.textLabel.text = "Blog" case (0, 2): cell.textLabel.text = "Contact Us" case (1, 0): cell.textLabel.text = “@nickoneill" case (1, 1): cell.textLabel.text = “@objctoswift" case (1, 2): cell.textLabel.text = “@whyareyousodumb" default: cell.textLabel.text = "¯\\_(ϑ)_/¯" } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
the best? swi* way enum TwitterHandles: Int { case Nickoneill
case Objctoswift case Whyareyousodumb func labelText() -> String { switch self { ... } } } let rowData = TwitterHandles(rawValue: indexPath.row) cell.textLabel.text = rowData.labelText() Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Our idea of the best Swi/ pa1erns will change over
8me Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Not every piece of clever code is a great pa0ern
Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
objec&ve-c dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // do some task dispatch_async(dispatch_get_main_queue(), ^{
// update some UI }); }); Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
swi$ func mainToBackground(background: () -> (), main: () -> ())
{ let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(priority, 0)) { background() dispatch_async(dispatch_get_main_queue()) { main() } } } mainToBackground({ // do some task }, { // update the ui }) Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
swi$? { /* do some task */ } ~> {
/* update some UI */} Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Great pa(erns provide convenience while maintaining clarity Discovering Na.ve Swi1
Pa3erns • June 9th, 2016
Custom Operators not even once Discovering Na.ve Swi1 Pa3erns •
June 9th, 2016
class ViewController: UIViewController { let imageView = UIImageView() let goButton
= UIButton() override func viewDidLoad() { imageView.image = UIImage(named: "profile") view.addSubview(imageView) goButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30) goButton.setTitle("GO!", forState: .Normal) view.addSubview(goButton) } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
class ViewController: UIViewController { let imageView: UIImageView = { let
imageView = UIImageView() imageView.image = UIImage(named: "profile") return imageView }() let goButton: UIButton = { let button = UIButton() button.frame = CGRect(x: 0, y: 0, width: 30, height: 30) button.setTitle("GO!", forState: .Normal) return button }() override func viewDidLoad() { view.addSubview(imageView) view.addSubview(goButton) } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
@IBOutlet weak var arrivalLabel: UILabel! { didSet { arrivalLabel.text =
"Arriving in 10 minutes".uppercaseString arrivalLabel.font = UIFont(name: "Lato", size: 11) arrivalLabel.textColor = UIColor.blueColor() arrivalLabel.textAlignment = .Center arrivalLabel.numberOfLines = 1 } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Swi$ pa(erns to make Swi$ be(er Discovering Na.ve Swi1 Pa3erns
• June 9th, 2016
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) if let paths =
tableView.indexPathsForSelectedRows { for path in paths { tableView.deselectRowAtIndexPath(path, animated: true) } } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
tableView.indexPathsForSelectedRows?.forEach({ (path) in tableView.deselectRowAtIndexPath(path, animated: true) }) Discovering Na.ve Swi1
Pa3erns • June 9th, 2016
tableView.indexPathsForSelectedRows?.forEach { tableView.deselectRowAtIndexPath($0, animated: true) } Discovering Na.ve Swi1 Pa3erns
• June 9th, 2016
Simple pa)erns can replace large dependencies Discovering Na.ve Swi1 Pa3erns
• June 9th, 2016
Unboxing1, or any JSON -> struct framework let json =
try? NSJSONSerialization.JSONObjectWithData(data, options: []) if let object = json as? Dictionary<String, AnyObject>, places: [Place] = Unbox(object) { return places } 1 h$ps:/ /github.com/JohnSundell/Unbox Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
struct Place: Unboxable { let id: String let address: Int?
let text: String init(unboxer: Unboxer) { self.id = unboxer.unbox("id") self.address = unboxer.unbox("address") self.placeName = unboxer.unbox("place_name") } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
struct Place { // ... init?(json: Dictionary<String, AnyObject>) { guard
let id = json["_id"] as? String else { return nil } self.id = id self.address = json["address"] as? Int self.placeName = (json["name"] as? String) ?? "No place name" } } Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Smart architecture decisions are just big pa4erns • Networking •
Core Data • No0fica0onCenter/KVO/delegates Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Tips for finding new pa0erns • Develop an intui.on for
code smell • Experiment in playgrounds or simplified projects • Re-read the language guide • Keep things clear Discovering Na.ve Swi1 Pa3erns • June 9th, 2016
Thanks! Ques%ons? Comments? @nickoneill •
[email protected]
Discovering Na.ve Swi1 Pa3erns
• June 9th, 2016