Skip to content

Instantly share code, notes, and snippets.

@akesson
Last active December 11, 2016 17:37
Show Gist options
  • Save akesson/f272172cb57b75623f59b5a82b9bf544 to your computer and use it in GitHub Desktop.
Save akesson/f272172cb57b75623f59b5a82b9bf544 to your computer and use it in GitHub Desktop.
Type Erasure Magic
// from Hector Matos speech Type Erasure Magic
//: https://realm.io/news/altconf-hector-matos-type-erasure-magic/
import UIKit
public struct Transaction {}
protocol CellReloadable: class {
associatedtype DataType
var selectedIndexPath: NSIndexPath? { get set }
var loadingCellObjects: [NSIndexPath : DataType] { get set }
func didFinishEditing(updated: Bool, newObject: DataType)
}
class AnyCellReloadable<ErasedDataType>: CellReloadable {
var selectedIndexPath: NSIndexPath?
var loadingCellObjects: [NSIndexPath : ErasedDataType] {
get { return _getLoadingCellObjects() }
set { _setLoadingCellObjects(newValue) }
}
private let _didFinishEditing: (Bool, ErasedDataType) -> Void
private let _getLoadingCellObjects: (Void) -> [NSIndexPath : ErasedDataType]
private let _setLoadingCellObjects: ([NSIndexPath : ErasedDataType]) -> Void
init<Injected: CellReloadable>(_ reloadable: Injected)
where Injected.DataType == ErasedDataType {
selectedIndexPath = reloadable.selectedIndexPath
_didFinishEditing = reloadable.didFinishEditing
_getLoadingCellObjects = { reloadable.loadingCellObjects }
_setLoadingCellObjects = { reloadable.loadingCellObjects = $0 }
}
func didFinishEditing(updated: Bool, newObject: ErasedDataType) {
}
}
class TransactionViewController: UIViewController {
var delegate: AnyCellReloadable<Transaction>?
override func viewDidLoad() {
}
func buttonTapped() {
delegate?.didFinishEditing(updated: true, newObject: Transaction())
navigationController?.popViewController(animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment