Skip to content

Instantly share code, notes, and snippets.

@artemnovichkov
Created March 12, 2020 12:29
Show Gist options
  • Save artemnovichkov/b31e369fd29ba43643f3cddc700c3c56 to your computer and use it in GitHub Desktop.
Save artemnovichkov/b31e369fd29ba43643f3cddc700c3c56 to your computer and use it in GitHub Desktop.
JSONDecoder + Result
import Foundation
public extension JSONDecoder {
func decode<T>(_ type: T.Type, from data: Data) -> Result<T, Error> where T: Decodable {
do {
let decodedData: T = try decode(T.self, from: data)
return .success(decodedData)
}
catch {
return .failure(error)
}
}
func decode<T>(_ result: Result<Data, Error>) -> Result<T, Error> where T: Decodable {
do {
let decodedData: T = try decode(T.self, from: try result.get())
return .success(decodedData)
}
catch {
return .failure(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment