Guest User

Untitled

a guest
Jan 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. extension KeyedDecodingContainer {
  2. func decode<T>(_ key: K, default: T) throws -> T where T: Codable {
  3. return try self.decodeIfPresent(T.self, forKey: key) ?? `default`
  4. }
  5.  
  6. func decode<T>(_ key: K) throws -> T where T: Codable {
  7. return try self.decode(T.self, forKey: key)
  8. }
  9. }
  10.  
  11. // Example
  12. let values = try decoder.container(keyedBy: CodingKeys.self)
  13. optionalName = try values.decode(.optionalName, default: "N/A")
  14. requiredName = try values.decode(.requiredName)
Add Comment
Please, Sign In to add comment