Created
October 24, 2018 18:32
-
-
Save jrmsklar/91d165e8efaab4fd600c96aef44edeb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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