View UITableView+Dequeue.swift
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
extension UITableView { | |
public func dequeue<T: UITableViewCell>(cellClass: T.Type) -> T? { | |
return dequeueReusableCell(withIdentifier: cellClass.reuseIdentifier) as? T | |
} | |
public func dequeue<T: UITableViewCell>(cellClass: T.Type, forIndexPath indexPath: IndexPath) -> T { | |
guard let cell = dequeueReusableCell( | |
withIdentifier: cellClass.reuseIdentifier, for: indexPath) as? T else { | |
fatalError( |
View UITableView+RegisterCell.swift
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
extension UITableView { | |
public func register<T: UITableViewCell>(cellClass: T.Type) { | |
register(cellClass, forCellReuseIdentifier: cellClass.reuseIdentifier) | |
} | |
} |
View UITableViewCell+ReuseIdentifier.swift
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
extension UITableViewCell { | |
static var reuseIdentifier: String { | |
return NSStringFromClass(self) | |
} | |
} |
View UIControlClosureUseage.swift
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
let button = UIButton() | |
button.addAction(for: .touchUpInside) { (button) in | |
// Run code | |
} | |
let slider = UISlider() | |
slider.addAction(for: .valueChanged) { (slider) in | |
// Run code | |
} |
View UIControlClosure.swift
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
extension UIControl { | |
/// Typealias for UIControl closure. | |
public typealias UIControlTargetClosure = (UIControl) -> () | |
private class UIControlClosureWrapper: NSObject { | |
let closure: UIControlTargetClosure | |
init(_ closure: @escaping UIControlTargetClosure) { | |
self.closure = closure | |
} |
View GOL-Forever.swift
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
let view = WorldView(worldSize: 100, cellSize: 5) | |
view.autoRun() | |
PlaygroundPage.current.liveView = view |
View GOL-Autorun.swift
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
public func autoRun() { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { | |
self.world.updateCells() | |
self.setNeedsDisplay() | |
self.autoRun() | |
} | |
} |
View GOL-Touches.swift
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
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
world.updateCells() | |
setNeedsDisplay() | |
} |
View GOL-CompleteSwitch.swift
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
switch livingNeighbors.count { | |
case 2...3 where cell.state == .alive: | |
updatedCells.append(cell) | |
case 3 where cell.state == .dead: | |
let liveCell = Cell(x: cell.x, y: cell.y, state: .alive) | |
updatedCells.append(liveCell) | |
default: | |
let deadCell = Cell(x: cell.x, y: cell.y, state: .dead) | |
updatedCells.append(deadCell) | |
} |
View GOL-DefaultCell.swift
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
default: | |
let deadCell = Cell(x: cell.x, y: cell.y, state: .dead) | |
updatedCells.append(deadCell) |
NewerOlder