Skip to content

Instantly share code, notes, and snippets.

@Austinate
Created December 20, 2017 16:36
Show Gist options
  • Save Austinate/c6be25d0925094cb9a524f63d154b233 to your computer and use it in GitHub Desktop.
Save Austinate/c6be25d0925094cb9a524f63d154b233 to your computer and use it in GitHub Desktop.
JSON Decoder which uses NSISO8601Formatter (or any other custom)
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