Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension KeyedDecodingContainer {
func decodeIfPresent<T: Decodable>(key: K) throws -> T? {
return try decodeIfPresent(T.self, forKey: key)
}
func decode<T: Decodable>(key: K) throws -> T {
return try decode(T.self, forKey: key)
}
}
@gokselkoksal

This comment has been minimized.

Owner

gokselkoksal commented May 26, 2018

struct Person {
  let name: String
}

Enables:

name = try container.decode(key: .name)

Instead of:

name = try container.decode(String.self, forKey: .name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment