Skip to content

Instantly share code, notes, and snippets.

@devxoul
Last active October 3, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/296cd45ea07660e7218c4a24975ea23a to your computer and use it in GitHub Desktop.
Save devxoul/296cd45ea07660e7218c4a24975ea23a to your computer and use it in GitHub Desktop.
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