Last active
October 3, 2019 17:24
-
-
Save devxoul/296cd45ea07660e7218c4a24975ea23a 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
class Cell { | |
private let viewBlock: () -> Void | |
init(viewBlock: @escaping () -> Void) { | |
self.viewBlock = viewBlock | |
} | |
deinit { | |
print(type(of: self), #function) | |
} | |
func loadView() { | |
self.viewBlock() | |
} | |
} | |
class DataSource { | |
private let configureCell: () -> Cell | |
init(configureCell: @escaping () -> Cell) { | |
self.configureCell = configureCell | |
} | |
deinit { | |
print(type(of: self), #function) | |
} | |
func cell() -> Cell { | |
let cell = self.configureCell() | |
cell.loadView() | |
return cell | |
} | |
} | |
class ViewController { | |
private let name: String = "A" | |
private lazy var dataSource: DataSource = DataSource(configureCell: { [weak self] in // 1: Required | |
// guard let self = self else { return ... } | |
return Cell(viewBlock: { [weak self] in // 2: Required only after strongify self | |
print(self?.name) | |
}) | |
}) | |
private var cells: [Cell] = [] | |
init() { | |
self.cells.append(self.dataSource.cell()) | |
} | |
deinit { | |
print(type(of: self), #function) | |
} | |
} | |
_ = { | |
_ = ViewController() | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment