Created
March 27, 2017 17:24
-
-
Save JohnSundell/cd1914169d01e402ecf95686a6ffc4de to your computer and use it in GitHub Desktop.
Code sample #3 from https://medium.com/@johnsundell/testing-swift-code-that-uses-system-singletons-in-3-easy-steps-89f4884cd325
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 DataLoader { | |
enum Result { | |
case data(Data) | |
case error(Error) | |
} | |
private let engine: NetworkEngine | |
init(engine: NetworkEngine = URLSession.shared) { | |
self.engine = engine | |
} | |
func load(from url: URL, completionHandler: @escaping (Result) -> Void) { | |
engine.performRequest(for: url) { (data, response, error) in | |
if let error = error { | |
return completionHandler(.error(error)) | |
} | |
completionHandler(.data(data ?? Data())) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment