Skip to content

Instantly share code, notes, and snippets.

@jrmsklar
Created October 24, 2018 18:32
Show Gist options
  • Save jrmsklar/91d165e8efaab4fd600c96aef44edeb3 to your computer and use it in GitHub Desktop.
Save jrmsklar/91d165e8efaab4fd600c96aef44edeb3 to your computer and use it in GitHub Desktop.
import UIKit
protocol ViewStateType {
var title: String { get set }
init()
}
protocol BaseViewDelegate: class {
associatedtype BaseViewType
func baseViewDidSomeAction(_ view: BaseViewType)
}
class BaseView<T: ViewStateType> {
var viewState: T = T(){
didSet {
update()
}
}
// weak var delegate: BaseViewDelegate? This does not work. If it did, everyone would be happy.
func update() {
print("BaesView<T> update()")
}
}
struct SubclassViewState: ViewStateType {
var title: String
var subtitle: String
init() {
title = "Default Title"
subtitle = "Default Subtitle"
}
}
class SubclassView: BaseView<SubclassViewState> {
override func update() {
super.update()
// viewState is a SubclassViewState
}
}
class SomeViewController: BaseViewDelegate {
func baseViewDidSomeAction(_ view: SubclassView) {
print("view is a SubclassView!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment