Last active
March 27, 2017 17:23
-
-
Save JohnSundell/85fd3b0f11e20cf65b442cdffd13926b to your computer and use it in GitHub Desktop.
Code sample #1 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) | |
} | |
func load(from url: URL, completionHandler: @escaping (Result) -> Void) { | |
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in | |
if let error = error { | |
return completionHandler(.error(error)) | |
} | |
completionHandler(.data(data ?? Data())) | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment