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
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
210
AIが無かった頃の素敵な出会いの話
codmoninc
1
420
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
680
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.6k
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
140
今さら聞けない .NET CLI
htkym
0
190
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
500
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
110
Go 1.27 における memory allocation の高速化
andpad
0
160
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
270
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
350
Android CLI
fornewid
0
220
Featured
See All Featured
From π to Pie charts
rasagy
0
270
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
260
How to Talk to Developers About Accessibility
jct
2
510
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
490
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Producing Creativity
orderedlist
PRO
348
40k
Making the Leap to Tech Lead
cromwellryan
135
10k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
290
Designing Powerful Visuals for Engaging Learning
tmiket
1
490
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