Skip to content

Instantly share code, notes, and snippets.

View TableViewCellGenerator.swift
class TableViewCellGenerator<T: UITableViewCell> {
// Class func - We need to override since each cell uses different objects to populate it self
class func configure(cell: UITableViewCell, using object: Any?, at indexPath: IndexPath) {}
// Static func - Should not be overriden since we use generics here
static func cell(for indexPath: IndexPath, inTableView tableView: UITableView) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath)
}
View TableViewCellGeneratorProtocol.swift
protocol TableViewCellGeneratorProtocol {
static func cell(for indexPath: IndexPath, inTableView tableView: UITableView) -> UITableViewCell
static func configure(cell: UITableViewCell, using object: Any?, at indexPath: IndexPath)
static func registerReuseIdentifier(for tableView: UITableView)
}
View ObjectInfoable.swift
protocol ObjectInfoable: NSObjectProtocol {}
extension ObjectInfoable {
static var fileName: String {
return String(describing: Self.self)
}
}
extension ObjectInfoable where Self: UIView {
static var nibName: UINib {
View TableViewDataSource.swift
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableStructure.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellType = tableStructure[indexPath.row]
let cell = cellType.generator.cell(for: indexPath, inTableView: tableView)
cellType.generator.configure(cell: cell, using: cellType.associatedObject, at: indexPath)
return cell
}
View CellType.swift
protocol CellType {
var generator: TableViewCellGeneratorProtocol.Type { get }
var associatedObject: Any? { get }
var cellHeight: CGFloat { get }
}
View CarFleet.swift
class CarFleet {
func moveCarsToPosition(cars: [Car], position: Position) {
cars.forEach({
if Toggles.Vehical.carsCanFly.enabled {
fly(to: location)
} else {
drive(to: location)
}
})
}
View CarFleetFeatureToggleLogic.swift
// MARK: - Toggle Logic For CarFleet
protocol CarFleetFeatureToggleLogic {
var fuelToUse: Fuel { get }
func transport(car: Car, to position: Position)
}
extension CarFleetFeatureToggleLogic where Self: CarFleet {
/**
Get the fuel to be used on cars.
View CarFleetCleaned.swift
class CarFleet: CarFleetFeatureToggleLogic {
func moveCarsToPosition(cars: [Car], position: Position) {
cars.forEach({ transport(car: $0, to: position) })
}
func refuel(cars: [Car]) {
cars.forEach({ refuel(car: $0, using: fuelToUse) })
}
func refuel(car: Car, using: Fuel) {
View Car.swift
class Car: Vehical {
// *Old* code Represented as the "False" state of the toggle
func drive(to geoLocation: Position) {
}
// *New* code Represented as the "True" state of the toggle
func fly(to geoLocation: Position) {
}
View guard example.swift
guard thisStatmentIsTrue else { assertionFailure("❌ Assert at: \(#line)") ; return }