Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokselkoksal/8bb3af6405e354a5958956d6bb78f2c5 to your computer and use it in GitHub Desktop.
Save gokselkoksal/8bb3af6405e354a5958956d6bb78f2c5 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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