Last active
December 5, 2021 21:34
-
-
Save philosopherdog/c76a40be1b65e572c07dc870b8bb5854 to your computer and use it in GitHub Desktop.
Simple Protocol Witness Example
This file contains hidden or 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 | |
struct ViewModel1 { | |
let title = "Hello" | |
} | |
struct ViewModel2 { | |
let message = "some message" | |
} | |
struct CellConfiguring<M, C: UICollectionViewCell> { | |
let config: (M, C) -> Void | |
} | |
class Cell1: UICollectionViewCell { | |
var label = UILabel() | |
var setup: CellConfiguring<ViewModel1, Cell1>? | |
} | |
class Cell2: UICollectionViewCell { | |
var message = UILabel() | |
var setup: CellConfiguring<ViewModel2, Cell2>? | |
} | |
extension CellConfiguring where M == ViewModel1, C == Cell1 { | |
static func witness() -> Self { | |
.init { d, c in | |
c.label.text = d.title | |
print(#line, d.title) | |
} | |
} | |
} | |
extension CellConfiguring where M == ViewModel2, C == Cell2 { | |
static func witness() -> Self { | |
.init { d, c in | |
c.message.text = d.message | |
print(#line, d.message) | |
} | |
} | |
} | |
let cell1 = Cell1(frame: .zero) | |
let model1 = ViewModel1() | |
CellConfiguring.witness().config(model1, cell1) | |
let cell2 = Cell2(frame: .zero) | |
let model2 = ViewModel2() | |
CellConfiguring.witness().config(model2, cell2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment