Created
August 19, 2018 18:17
-
-
Save tjeerdintveen/0ffd1d477937f17114dce3a51d374d0f to your computer and use it in GitHub Desktop.
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
extension URLSession { | |
func dataTask(with url: URL, completionHandler: @escaping (Result<Data, AnyError>) -> Void) -> URLSessionTask { | |
return dataTask(with: url, completionHandler: { (data, response, error) in | |
if let error = error { | |
let anyError = AnyError(error) | |
completionHandler(Result.failure(anyError)) | |
} else if let data = data { | |
completionHandler(Result.success(data)) | |
} else { | |
let anyError = AnyError(NSError(domain: "my domain", code: 500, userInfo: nil)) | |
completionHandler(Result.failure(anyError)) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This extension gives you the ability to use Apple's Result on URLSession. The Result type can be found at https://github.com/apple/swift-package-manager/blob/master/Sources/Basic/Result.swift