Skip to content

Instantly share code, notes, and snippets.

@boska
Created May 18, 2017 09:41
Show Gist options
  • Save boska/02204f35f7f16357d2a195a647e8350d to your computer and use it in GitHub Desktop.
Save boska/02204f35f7f16357d2a195a647e8350d to your computer and use it in GitHub Desktop.
extension URL {
func fetch(completionHandler: ((Data) -> ())? = nil) {
print("Request: \(self)")
URLSession.shared.dataTask(with: self) {
guard
let response = $1 as? HTTPURLResponse, response.statusCode == 200,
let data = $0, $2 == nil
else { return }
completionHandler?(data)
}.resume()
}
}
let url = URL()
url.fetch {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment