How to convert a date string with optional fractional seconds using Codable in Swift?

You can use two different date formatters (with and without fraction seconds) and create a custom DateDecodingStrategy. In case of failure when parsing the date returned by the API you can throw a DecodingError as suggested by @PauloMattos in comments: iOS 9, macOS 10.9, tvOS 9, watchOS 2, Xcode 9 or later The custom ISO8601 … Read more

How do I use custom keys with Swift 4’s Decodable protocol?

Manually customising coding keys In your example, you’re getting an auto-generated conformance to Codable as all your properties also conform to Codable. This conformance automatically creates a key type that simply corresponds to the property names – which is then used in order to encode to/decode from a single keyed container. However one really neat … Read more