Skip to content

Instantly share code, notes, and snippets.

@salmaanahmed
Created January 28, 2019 11:31
Show Gist options
  • Save salmaanahmed/9114b6acdba148c85515c946b8b46704 to your computer and use it in GitHub Desktop.
Save salmaanahmed/9114b6acdba148c85515c946b8b46704 to your computer and use it in GitHub Desktop.
This API expresses exactly the intended result (either an error or data and response, never all or none) and allows them to be handled much more clearly.
URLSession.shared.dataTask(with: url) { (result: Result<(response: URLResponse, data: Data), Error>) in // Type added for illustration purposes.
switch result {
case let .success(success):
handleResponse(success.response, data: success.data)
case let .error(error):
handleError(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment