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
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.3k
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
140
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
530
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
250
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1.5k
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
8k
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
190
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.3k
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
560
T3DD26: From RAGs to Riches
martinhelmich
0
130
仕様駆動開発の消費期限
watany
20
9.2k
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
130
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
97
6.8k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
460
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
230
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
480
Paper Plane
katiecoart
PRO
2
53k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Darren the Foodie - Storyboard
khoart
PRO
3
3.8k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
690
Deep Space Network (abreviated)
tonyrice
0
280
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
180
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