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 ModelLoader<T: Unboxable & Requestable> { | |
func load(completionHandler: (Result<T>) -> Void) { | |
networkService.loadData(from: T.requestURL) { data in | |
do { | |
try completionHandler(.success(unbox(data: data))) | |
} catch { | |
let error = ModelLoadingError.unboxingFailed(error) | |
completionHandler(.error(error)) | |
} | |
} |
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
protocol ModelLoading { | |
associatedtype Model | |
func load(completionHandler: (Result<Model>) -> Void) | |
} |
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 ViewController: UIViewController { | |
init(modelLoader: ModelLoading) { | |
... | |
} | |
} |
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 ViewController: UIViewController { | |
init<T: ModelLoading>(modelLoader: T) where T.Model == MyModel { | |
... | |
} | |
} |
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 AnyModelLoader<T>: ModelLoading { | |
typealias CompletionHandler = (Result<T>) -> Void | |
private let loadingClosure: (CompletionHandler) -> Void | |
init<L: ModelLoading>(loader: L) where L.Model == T { | |
loadingClosure = loader.load | |
} | |
func load(completionHandler: CompletionHandler) { |
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 ViewController: UIViewController { | |
private let modelLoader: AnyModelLoader<MyModel> | |
init<T: ModelLoading>(modelLoader: T) where T.Model == MyModel { | |
self.modelLoader = AnyModelLoader(loader: modelLoader) | |
super.init(nibName: nil, bundle: nil) | |
} | |
} |
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 ViewController: UIViewController { | |
private let loadModel: ((Result<MyModel>) -> Void) -> Void | |
init<T: ModelLoading>(modelLoader: T) where T.Model == MyModel { | |
loadModel = modelLoader.load | |
super.init(nibName: nil, bundle: nil) | |
} | |
} |
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
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
loadModel { result in | |
switch result { | |
case .success(let model): | |
render(model) | |
case .error(let error): | |
render(error) | |
} |
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 ModelLoader { | |
func loadAllModels() -> [Model] { … } | |
} |
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
struct ModelSequence: Sequence { | |
func makeIterator() -> ModelIterator { | |
return ModelIterator() | |
} | |
} |