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
Type-safe Oriented Programming
Search
Diego Ventura
April 27, 2016
Technology
0
31
Type-safe Oriented Programming
Talk da 17ª edição do CocoaHeads RJ sobre Type-safe Oriented Programming
Diego Ventura
April 27, 2016
Tweet
Share
Other Decks in Technology
See All in Technology
現実世界の事象から学ぶSOLID原則
h0r15h0
24
10k
SREsのためのSRE定着ガイド
netmarkjp
10
1.6k
Azureコストは水道代/The_47th_Tokyo_Jazug
aeonpeople
3
360
10分でわかるfreeeのQA
freee
0
230
AWS アーキテクチャクイズ
yuu26
2
700
TypeScript Quiz (Encraft #12 Frontend Quiz Night)
uhyo
4
410
家族アルバム みてねで直面してきた技術的負債 / MIXI KAG 2024
isaoshimizu
17
7.7k
Challenges - Open Farming Hackdays 2024
loleg
0
560
TCA入門したてなので、自分が馴染みのある実装と比較しながらキャッチアップしてみる
fumiyasac0921
1
370
バッチ処理のSLOをどう設計するか
rynsuke
7
560
Oracle Database で機械学習を始めよう! Oracle Machine Learning
oracle4engineer
PRO
1
140
2023 Japan AWS Jr.Championsに選出されての振り返りとこれから
hiropy877
1
130
Featured
See All Featured
A designer walks into a library…
pauljervisheath
199
23k
The Art of Programming - Codeland 2020
erikaheidi
40
12k
Large-scale JavaScript Application Architecture
addyosmani
501
110k
Fantastic passwords and where to find them - at NoRuKo
philnash
35
2.4k
Raft: Consensus for Rubyists
vanstee
130
6.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
185
15k
Building Adaptive Systems
keathley
29
1.8k
Agile that works and the tools we love
rasmusluckow
323
20k
Producing Creativity
orderedlist
PRO
335
39k
The Brand Is Dead. Long Live the Brand.
mthomps
48
21k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
101
6.6k
Why You Should Never Use an ORM
jnunemaker
PRO
50
8.6k
Transcript
Type-safe oriented programming: Fuja das Strings! Diego Ventura iOS Developer
@ OLX Brasil
E no início eram os xibs…
E no início eram os xibs… • XIB - XML
Interface Builder / Mac OS X Interface Builder • File > New File > View • File’s owner (UIViewController)
E no início eram os xibs… Objective-C Swift [[ViewController alloc]
initWithNibName:@"ViewController" bundle:nil]; ViewController(nibName: "ViewController", bundle: nil)
E depois veio o Storyboard…
E depois veio o Storyboard… • Vários Xibs num mesmo
arquivo • Set subclass na ViewController • Segues facilitaram a vida dos devs
E depois veio o Storyboard… Objective-C Swift UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; YourViewControllerClass *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; ViewController(nibName: "ViewController", bundle: nil)
Só tem um problema TODOS eles usam Strings
Strings são ruins • Não possui verificação em compile time
• Em times grandes, é mais complicado ter uma padronização • Favorece, e muito, o typo! (i.e MyTypoViewControler)
E aí como faz?
E aí como faz?
E aí como faz?
Type-safe w/ Extensions extension UIViewController { private class var nibNameIdentifier:
String { return NSStringFromClass(self).componentsSeparatedByString(".").last! } class func instantiateFromXib() -> Self { return self.init(nibName: nibNameIdentifier, bundle: nil) } } let welcomeViewController = WelcomeViewController.instantiateFromXib()
Type-safe w/ Extensions
Type-safe w/ Extensions and Generics //Register nib let nib =
UINib(nibName: "MyCustomCellNib", bundle: nil) tableView.registerNib(nib, forCellReuseIdentifier: "MyCustomCellIdentifier") //Dequeue cell var cell = tableView.dequeueReusableCellWithIdentifier(“MyCustomCellIdentifier", forIndexPath: indexPath) as MyCell!
Type-safe w/ Extensions and Generics
Type-safe w/ Extensions and Generics protocol ReusableViewProtocol { static var
reuseIdentifier: String { get } } extension ReusableViewProtocol where Self: UIView { static var reuseIdentifier: String { return NSStringFromClass(self) } }
Type-safe w/ Extensions and Generics protocol ReusableNibLoadableProtocol: class { static
var nibName: String { get } } extension ReusableNibLoadableProtocol where Self: UIView { static var nibName: String { return NSStringFromClass(self).componentsSeparatedByString(".").last! } }
Type-safe w/ Extensions and Generics extension UITableView { func registerClass<T:
UITableViewCell where T: ReusableViewProtocol>(_: T.Type) { registerClass(T.self, forCellReuseIdentifier: T.reuseIdentifier) } func registerNib<T: UITableViewCell where T: ReusableViewProtocol, T: ReusableNibLoadableProtocol>(_: T.Type) { let bundle = NSBundle(forClass: T.self) let nib = UINib(nibName: T.nibName, bundle: bundle) registerNib(nib, forCellReuseIdentifier: T.reuseIdentifier) } }
Demo
Perguntas
Obrigado! Diego Ventura
[email protected]
@venturadiego https://github.com/diegoventura @ PS: Estou vendendo
um MacBook Air