Created
December 20, 2017 16:36
-
-
Save Austinate/c6be25d0925094cb9a524f63d154b233 to your computer and use it in GitHub Desktop.
JSON Decoder which uses NSISO8601Formatter (or any other custom)
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 JSONDecoder.DateDecodingStrategy { | |
static var iso8601Custom: JSONDecoder.DateDecodingStrategy { | |
return .custom { decoder in | |
let container = try decoder.singleValueContainer() | |
let string = try container.decode(String.self) | |
guard let date = DateFormatter.iso8601Mapping.date(from: string) else { | |
throw DecodingError.dataCorruptedError(in: container, | |
debugDescription: "Date is in invalid format") | |
} | |
return date | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment