Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Protocols and The Promised Land Take 2
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Michele
March 02, 2016
Programming
77
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
Transcript
51 slides
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/
More Decks by Michele
See All by Michele
Protocols and the Promised Land
mtitolo
1
15k
Other Decks in Programming
See All in Programming
はじめてのKubernetesコントリビューション
nasa9084
0
330
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
17
9.6k
Apache Airflow Introduction
leew
1
100
数行の変更でDockerビルドを最大91%高速化~GitLab CIの実行基盤を社内40リポジトリに展開するまで~
picopico
3
810
Canvas 2D Context Next
petamoriken
0
190
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
150
ruby/uri meets WHATWG
jinroq
0
150
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
160
Crash Auto Pipelines
veronikapj
1
450
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
220
AIエージェントの「テストは通るのに本番で壊れる」を減らす
shibuiwilliam
3
130
Stage 3でも仕様は変わる。アプリケーション開発者のためのTC39プロセスの読み方
bicstone
1
280
Featured
See All Featured
How to build a perfect <img>
jonoalderson
1
6.3k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
550
WENDY [Excerpt]
tessaabrams
15
41k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
690
Building Adaptive Systems
keathley
44
3.3k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
580
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
590