Skip to content

Instantly share code, notes, and snippets.

@ha1f
Created February 18, 2021 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ha1f/3f67a171d0ce47814343f399bcc67c7a to your computer and use it in GitHub Desktop.
Save ha1f/3f67a171d0ce47814343f399bcc67c7a to your computer and use it in GitHub Desktop.
public extension KeyedDecodingContainerProtocol {
func decode<T: Decodable>(forKey key: Key) throws -> T {
try decode(T.self, forKey: key)
}
func decodeSafe<T: Decodable>(forKey key: Key, defaultValue: T) -> T {
(try? decode(T.self, forKey: key)) ?? defaultValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment