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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
Inside Stream API
skrb
1
800
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
200
Creating Composable Callables in Contemporary C++
rollbear
0
170
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1k
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.6k
Webフレームワークの ベンチマークについて
yusukebe
0
180
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
200
Vite+ Unified Toolchain for the Web
naokihaba
0
360
Oxcを導入して開発体験が向上した話
yug1224
4
340
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.3k
act1-costs.pdf
sumedhbala
0
120
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
0
160
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
58k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
ラッコキーワード サービス紹介資料
rakko
1
3.8M
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
350
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
450
Abbi's Birthday
coloredviolet
3
8.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
How GitHub (no longer) Works
holman
316
150k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
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