SlideShare a Scribd company logo
POMVVM@NATASHATHEROBOT
Protocol-Oriented MVVM
Protocol-Oriented MVVM
"Swift Is a Protocol-Oriented
Programming Language"
— Dave Abrahams, Professor of Blowing-Your-Mind
UITableViewDelegate
UITableViewDataSource
UITextFieldDelegate
NSURLSessionDelegate
CLLocationManagerDelegate
MCSessionDelegate
!
!
!
!
Artsy Engineering: MVVM in Swift
Protocol-Oriented MVVM
THE PROBLEM
class SwitchWithTextTableViewCell: UITableViewCell {
func configure(
title: String,
titleFont: UIFont,
titleColor: UIColor,
switchOn: Bool,
switchColor: UIColor = .purpleColor(),
onSwitchToggleHandler: onSwitchToggleHandlerType? = nil)
{
// configure views here
}
}
PROTOCOLS TO
THE RESCUE !
protocol SwitchWithTextCellProtocol {
var title: String { get }
var titleFont: UIFont { get }
var titleColor: UIColor { get }
var switchOn: Bool { get }
var switchColor: UIColor { get }
func onSwitchTogleOn(on: Bool)
}
extension SwitchWithTextCellProtocol {
var switchColor: UIColor {
return .purpleColor()
}
}
class SwitchWithTextTableViewCell: UITableViewCell {
func configure(withDelegate delegate: SwitchWithTextCellProtocol)
{
// configure views here
}
}
struct MinionModeViewModel: SwitchWithTextCellProtocol {
var title = "Minion Mode!!!"
var switchOn = true
var switchColor: UIColor {
return .yellowColor()
}
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
CELLFORROWATINDEXPATH
// YourViewController.swift
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell",
forIndexPath: indexPath) as! SwitchWithTextTableViewCell
// this is where the magic happens!
cell.configure(withDelegate: MinionModeViewModel())
return cell
!
protocol SwitchWithTextCellDataSource {
var title: String { get }
var switchOn: Bool { get }
}
protocol SwitchWithTextCellDelegate {
func onSwitchTogleOn(on: Bool)
var switchColor: UIColor { get }
var textColor: UIColor { get }
var font: UIFont { get }
}
// SwitchWithTextTableViewCell
func configure(withDataSource dataSource: SwitchWithTextCellDataSource,
delegate: SwitchWithTextCellDelegate?)
{
// configure views here
}
struct MinionModeViewModel: SwitchWithTextCellDataSource {
var title = "Minion Mode!!!"
var switchOn = true
}
extension MinionModeViewModel: SwitchWithTextCellDelegate {
var switchColor: UIColor {
return .yellowColor()
}
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
// SettingsViewController
let viewModel = MinionModeViewModel()
cell.configure(withDataSource: viewModel, delegate: viewModel)
return cell
!
@MHOLLEMANS: MIXINS
AND TRAITS IN SWIFT 2.0
Protocol-Oriented MVVM
Protocol-Oriented MVVM
class AIPlayer: GameObject, AITrait, GunTrait, RenderTrait, HealthTrait {
...
}
class ZapMonster: GameObject, GunTrait, RenderTrait, HealthTrait, MovementTrait {
...
}
! "
protocol TextPresentable {
var text: String { get }
var textColor: UIColor { get }
var font: UIFont { get }
}
protocol SwitchPresentable {
var switchOn: Bool { get }
var switchColor: UIColor { get }
func onSwitchTogleOn(on: Bool)
}
protocol ImagePresentable {
var imageName: String { get }
}
protocol TextFieldPresentable {
var placeholder: String { get }
var text: String { get }
func onTextFieldDidEndEditing(textField: UITextField)
}
extension TextPresentable {
var textColor: UIColor {
return .blackColor()
}
var font: UIFont {
return .systemFontOfSize(17)
}
}
!!!
class SwitchWithTextTableViewCell<T where T: TextPresentable, T: SwitchPresentable>: UITableViewCell {
private var delegate: T?
func configure(withDelegate delegate: T) {
// configure views here
}
}
extension MinionModeViewModel: TextPresentable {
var text: String { return "Minion Mode" }
var textColor: UIColor { return .blackColor() }
var font: UIFont { return .systemFontOfSize(17.0) }
}
extension MinionModeViewModel: SwitchPresentable {
var switchOn: Bool { return false }
var switchColor: UIColor { return .yellowColor() }
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
}
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchWithTextTableViewCell",
forIndexPath: indexPath) as! SwitchWithTextTableViewCell<MinionModeViewModel>
let viewModel = MinionModeViewModel()
cell.configure(withDelegate: viewModel)
return cell
!"#
"Change is the only constant."
— Unknown
!"
> Use Protocols to Configure Your Views
> Use Protocol Extensions for Defaults
> Use ViewModels to Provide Data for the Protocols
HOW CAN WE
MAKE THIS
BETTER?

More Related Content

What's hot (20)

PPTX
Workshop 1: Good practices in JavaScript
Visual Engineering
 
PDF
Workshop 17: EmberJS parte II
Visual Engineering
 
PDF
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
KEY
Gwt and Xtend
Sven Efftinge
 
PDF
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
PPTX
Vue next
Vitalii Ratyshnyi
 
PDF
IoT Best practices
CocoaHeads France
 
PDF
Testing Backbone applications with Jasmine
Leon van der Grient
 
PDF
Workshop 19: ReactJS Introduction
Visual Engineering
 
PDF
Codegeneration With Xtend
Sven Efftinge
 
PDF
Workshop 5: JavaScript testing
Visual Engineering
 
PDF
Intro to JavaScript
Jussi Pohjolainen
 
PDF
Workshop 24: React Native Introduction
Visual Engineering
 
PDF
Workshop 25: React Native - Components
Visual Engineering
 
PDF
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
PDF
Workshop 2: JavaScript Design Patterns
Visual Engineering
 
PDF
Advanced javascript
Doeun KOCH
 
PDF
Reactive, component 그리고 angular2
Jeado Ko
 
PDF
Understanding Asynchronous JavaScript
jnewmanux
 
PDF
How to build to do app using vue composition api and vuex 4 with typescript
Katy Slemon
 
Workshop 1: Good practices in JavaScript
Visual Engineering
 
Workshop 17: EmberJS parte II
Visual Engineering
 
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
Gwt and Xtend
Sven Efftinge
 
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
IoT Best practices
CocoaHeads France
 
Testing Backbone applications with Jasmine
Leon van der Grient
 
Workshop 19: ReactJS Introduction
Visual Engineering
 
Codegeneration With Xtend
Sven Efftinge
 
Workshop 5: JavaScript testing
Visual Engineering
 
Intro to JavaScript
Jussi Pohjolainen
 
Workshop 24: React Native Introduction
Visual Engineering
 
Workshop 25: React Native - Components
Visual Engineering
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
Workshop 2: JavaScript Design Patterns
Visual Engineering
 
Advanced javascript
Doeun KOCH
 
Reactive, component 그리고 angular2
Jeado Ko
 
Understanding Asynchronous JavaScript
jnewmanux
 
How to build to do app using vue composition api and vuex 4 with typescript
Katy Slemon
 

Viewers also liked (20)

PDF
Build Features, Not Apps
Natasha Murashev
 
PDF
The Secret Life of a Digital Nomad
Natasha Murashev
 
PDF
Swift Delhi: Practical POP
Natasha Murashev
 
PDF
How to Win on the Apple Watch
Natasha Murashev
 
PDF
Build Features Not Apps
Natasha Murashev
 
PDF
Hello watchOS2
Natasha Murashev
 
PDF
The Swift Architect
Natasha Murashev
 
PDF
The Zen Guide to WatchOS 2
Natasha Murashev
 
PDF
Build Features Not Apps
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
Hello, WatchKit
Natasha Murashev
 
PDF
HealthKit Deep Dive
Natasha Murashev
 
PDF
Using Parse in Hackathons
Natasha Murashev
 
PDF
Unleash the Power of Playgrounds
Natasha Murashev
 
PDF
Practical Protocol-Oriented-Programming
Natasha Murashev
 
PDF
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
PDF
UX, ethnography and possibilities: for Libraries, Museums and Archives
Ned Potter
 
PDF
Hype vs. Reality: The AI Explainer
Luminary Labs
 
PDF
Visual Design with Data
Seth Familian
 
Build Features, Not Apps
Natasha Murashev
 
The Secret Life of a Digital Nomad
Natasha Murashev
 
Swift Delhi: Practical POP
Natasha Murashev
 
How to Win on the Apple Watch
Natasha Murashev
 
Build Features Not Apps
Natasha Murashev
 
Hello watchOS2
Natasha Murashev
 
The Swift Architect
Natasha Murashev
 
The Zen Guide to WatchOS 2
Natasha Murashev
 
Build Features Not Apps
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
Hello, WatchKit
Natasha Murashev
 
HealthKit Deep Dive
Natasha Murashev
 
Using Parse in Hackathons
Natasha Murashev
 
Unleash the Power of Playgrounds
Natasha Murashev
 
Practical Protocol-Oriented-Programming
Natasha Murashev
 
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
UX, ethnography and possibilities: for Libraries, Museums and Archives
Ned Potter
 
Hype vs. Reality: The AI Explainer
Luminary Labs
 
Visual Design with Data
Seth Familian
 
Ad

Similar to Protocol-Oriented MVVM (20)

PDF
Swift - One step forward from Obj-C
Nissan Tsafrir
 
PPT
Smoothing Your Java with DSLs
intelliyole
 
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Sencha
 
PDF
Building mobile web apps with Mobello
Jeong-Geun Kim
 
PDF
Clean coding-practices
John Ferguson Smart Limited
 
PPT
Laurens Van Den Oever Xopus Presentation
Ajax Experience 2009
 
PPT
JS everywhere 2011
Oleg Podsechin
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPTX
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
PDF
cq_cxf_integration
Ankur Chauhan
 
PDF
From Legacy to Hexagonal (An Unexpected Android Journey)
Jose Manuel Pereira Garcia
 
PDF
Griffon @ Svwjug
Andres Almiray
 
PDF
Vb script tutorial for qtp[1]
srikanthbkm
 
PPT
Sqlapi0.1
jitendral
 
PDF
DataFX - JavaOne 2013
Hendrik Ebbers
 
PDF
Need help coding MorseCode in JavaCreate Class MorseCodeClient. T.pdf
fastechsrv
 
PDF
Effective JavaFX architecture with FxObjects
Srikanth Shenoy
 
PPTX
Kotlin Coroutines and Rx
Shaul Rosenzwieg
 
PPTX
Module Ninja .JS
Александър Динков
 
PDF
Presto anatomy
Dongmin Yu
 
Swift - One step forward from Obj-C
Nissan Tsafrir
 
Smoothing Your Java with DSLs
intelliyole
 
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Sencha
 
Building mobile web apps with Mobello
Jeong-Geun Kim
 
Clean coding-practices
John Ferguson Smart Limited
 
Laurens Van Den Oever Xopus Presentation
Ajax Experience 2009
 
JS everywhere 2011
Oleg Podsechin
 
Introduction to Javascript
Amit Tyagi
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
cq_cxf_integration
Ankur Chauhan
 
From Legacy to Hexagonal (An Unexpected Android Journey)
Jose Manuel Pereira Garcia
 
Griffon @ Svwjug
Andres Almiray
 
Vb script tutorial for qtp[1]
srikanthbkm
 
Sqlapi0.1
jitendral
 
DataFX - JavaOne 2013
Hendrik Ebbers
 
Need help coding MorseCode in JavaCreate Class MorseCodeClient. T.pdf
fastechsrv
 
Effective JavaFX architecture with FxObjects
Srikanth Shenoy
 
Kotlin Coroutines and Rx
Shaul Rosenzwieg
 
Presto anatomy
Dongmin Yu
 
Ad

More from Natasha Murashev (10)

PDF
Digital Nomad: The New Normal
Natasha Murashev
 
PDF
AltConf 2015: Swift Thinking
Natasha Murashev
 
PDF
Swift Thinking
Natasha Murashev
 
PDF
Intro To Swift
Natasha Murashev
 
PDF
HealthKit: Getting Ready for the New Year
Natasha Murashev
 
PDF
Learning.... Swift functions!
Natasha Murashev
 
PDF
A Swift introduction a.k.a Fun with Minions!
Natasha Murashev
 
PDF
The Many Faces of Swift Functions
Natasha Murashev
 
PDF
Getting Started with Open Source
Natasha Murashev
 
PDF
iOSDevCampDC: A Swift Introduction
Natasha Murashev
 
Digital Nomad: The New Normal
Natasha Murashev
 
AltConf 2015: Swift Thinking
Natasha Murashev
 
Swift Thinking
Natasha Murashev
 
Intro To Swift
Natasha Murashev
 
HealthKit: Getting Ready for the New Year
Natasha Murashev
 
Learning.... Swift functions!
Natasha Murashev
 
A Swift introduction a.k.a Fun with Minions!
Natasha Murashev
 
The Many Faces of Swift Functions
Natasha Murashev
 
Getting Started with Open Source
Natasha Murashev
 
iOSDevCampDC: A Swift Introduction
Natasha Murashev
 

Recently uploaded (20)

PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 

Protocol-Oriented MVVM