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
Protocols and The Promised Land Take 2
Search
Michele
March 02, 2016
Programming
76
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Protocols and The Promised Land Take 2
try! Swift edition
Michele
March 02, 2016
More Decks by Michele
See All by Michele
Protocols and the Promised Land
mtitolo
1
15k
Other Decks in Programming
See All in Programming
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
160
継続モナドとリアクティブプログラミング
yukikurage
3
620
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
410
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
360
どこまでゆるくて許されるのか
tk3fftk
0
510
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
180
FDEが実現するAI駆動経営の現在地
gonta
1
110
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
230
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
150
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
2
580
yield再入門 #phpcon
o0h
PRO
0
630
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
600
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Skip the Path - Find Your Career Trail
mkilby
1
170
BBQ
matthewcrist
89
10k
Leo the Paperboy
mayatellez
8
1.9k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
150
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
72
40k
Claude Code のすすめ
schroneko
67
230k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
650
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
900
Thoughts on Productivity
jonyablonski
76
5.3k
sira's awesome portfolio website redesign presentation
elsirapls
0
300
Transcript
Protocols and the Promised Land @MicheleTitolo
Swift is shiny! And new!
Swift is not Objective-C
Interop
as? as? as?
as? lies
let vendor = ObjcObject() let items = vendor.giveMeItems() as? [Fruit]
let vendor = ObjcObject() let items = vendor.giveMeItems().map { $0
as Fruit }
@objc spreads like a virus
@objc protocol SomeProtocol { func someFunc() } class MyClass: SomeProtocol
{}
@objc protocol SomeProtocol { func someFunc() } @objc class MyClass:
NSObject, SomeProtocol {}
Swift > Objc > Swift
Can’t #import “MyApp-Swift.h” in header
Can’t conform, can’t subclass
Bridging
many parts of Swift don’t bridge
Does this really need to be used in Objective-C?
framework headers can’t be in bridging header
use @import
Partial bridging
-Swift.h will leave out funcs/vars that don’t bridge
Again: does this really need to be bridged?
Type System
properties can have 1 type
protocol Themeable {} class ListViewController: UIViewController, Themeable {} var themedViewController:
// UIViewController, Themeable ????
two workarounds
public protocol ViewControllerProtocol { var view: UIView! { get }
var storyboard: UIStoryboard? { get } init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) func viewDidLoad() // etc } protocol ThemeableViewController: Themeable, ViewControllerProtocol {} var themedViewController: ThemeableViewController Swift-y but hack-y
var themed: Themeable { get { return self.viewController as! Themeable
} set(newThemeable) { if let themedViewController = newThemeable as? UIViewController{ viewController = themedViewController } } } var viewController: UIViewController init<T where T: Themeable, T: UIViewController>(viewController: T) { self.viewController = viewController } Also swfit-y also hack-y
Can’t override type with inheritance
Deep object hierarchies
Protocols
Composition instead of inheritance
Protocols and inheritance don’t mix
use POP to mock
Generics
Can’t reference a generic protocol
protocol Holder { typealias Item var items: [Item] { get
} } class Basket<Thing>: Holder{ var items: [Thing] = [] func add(item: Thing) { items.append(item) } }
var someHolder: Holder var someBasket: Basket
var someHolder: Holder<Peach> var someBasket: Basket<Peach>
Covariance only works with generic classes
Covariance: If a Cat is an Animal func feed(animal: Animal)
can receive a Cat
typealias Object typealias Object: String typealias RoundObject: Object
generic classes can specify type requirements
class Basket<Thing: Fruit> { var items: [Thing] = [] func
add(item: Thing) { items.append(item) } }
class Basket<Thing: Fruit> { var items: [Thing] = [] func
add(item: Thing) { items.append(item) } } class FruitBasket: Basket<Fruit> {} class PeachBasket: FruitBasket<Peach> {}
class Bag<Stuff> { class Basket<Thing: Stuff> { var items: [Thing]
= [] func add(item: Thing) { items.append(item) } } }
Generic non-NSObjects have issues
Explicitly call out class for now
Swift introduces awesome new patterns
Thanks! @MicheleTitolo
Photo Credits • https://www.flickr.com/photos/83073191@N00/21709516808/ • https://www.flickr.com/photos/64181484@N04/21946459678/ • https://www.flickr.com/photos/66904032@N05/18905223864/ • https://www.flickr.com/photos/27454212@N00/22120629112/
• https://www.flickr.com/photos/8725928@N02/24439195435/ • https://www.flickr.com/photos/28480761@N04/24143685631/