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
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
0
250
どこまでゆるくて許されるのか
tk3fftk
0
520
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
110
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
340
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
320
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
1k
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
7
2.8k
霧の中の代数的エフェクト
funnyycat
1
410
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.6k
SLOをサービス品質の共通言語にするために 取り組んできたこと
wakana0222
0
540
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
200
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
140
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
620
The Cult of Friendly URLs
andyhume
79
7k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
260
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
It's Worth the Effort
3n
188
29k
Scaling GitHub
holman
464
140k
We Are The Robots
honzajavorek
0
280
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
420
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
A Tale of Four Properties
chriscoyier
163
24k
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