Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension KeyedDecodingContainer {
- func decode<T>(_ key: K, default: T) throws -> T where T: Codable {
- return try self.decodeIfPresent(T.self, forKey: key) ?? `default`
- }
- func decode<T>(_ key: K) throws -> T where T: Codable {
- return try self.decode(T.self, forKey: key)
- }
- }
- // Example
- let values = try decoder.container(keyedBy: CodingKeys.self)
- optionalName = try values.decode(.optionalName, default: "N/A")
- requiredName = try values.decode(.requiredName)
Add Comment
Please, Sign In to add comment