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
/** | |
* Copyright (c) John Sundell 2017 | |
* Licensed under the MIT license | |
*/ | |
import Foundation | |
import XCTest | |
/** | |
* Assert that an expression throws a given 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
func loadSearchData(matching query: String) throws -> Data { | |
let urlString = "https://my.api.com/search?q=\(query)" | |
guard let url = URL(string: urlString) else { | |
throw SearchError.invalidQuery(query) | |
} | |
return try Data(contentsOf: url) | |
} |
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
func loadSearchData(matching query: String) throws -> Data { | |
let urlString = "https://my.api.com/search?q=\(query)" | |
guard let url = URL(string: urlString) else { | |
throw SearchError.invalidQuery(query) | |
} | |
do { | |
return try Data(contentsOf: url) | |
} catch { |
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
func perform<T>(_ expression: @autoclosure () throws -> T, | |
orThrow error: Error) throws -> T { | |
do { | |
return try expression() | |
} catch { | |
throw 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
func loadSearchData(matching query: String) throws -> Data { | |
let urlString = "https://my.api.com/search?q=\(query)" | |
guard let url = URL(string: urlString) else { | |
throw SearchError.invalidQuery(query) | |
} | |
return try perform(Data(contentsOf: url), | |
orThrow: SearchError.dataLoadingFailed(url)) | |
} |
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) { |
OlderNewer