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
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
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
350
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
220
ActionController meets Protocol Buffers
sorah
0
530
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.5k
カツオ、ご期待ください
suneo3476
0
190
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
2
560
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
370
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
110
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
930
Practical Orchestrator
shlominoach
192
12k
What's in a price? How to price your products and services
michaelherold
247
13k
Navigating Team Friction
lara
192
16k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
800
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
540
Embracing the Ebb and Flow
colly
88
5.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
WCS-LA-2024
lcolladotor
0
890
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
360
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/