Created
April 8, 2019 02:52
-
-
Save juliantejera/781a566f8832ce9aa7710e07f30127c6 to your computer and use it in GitHub Desktop.
Decoding Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension KeyedDecodingContainer where K : CodingKey { | |
func decodeArrayWithOptionalElements<T>(_ type: [T].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [T] where T : Decodable { | |
return (try decode([DecodableContainer<T>].self, forKey: key)).compactMap { $0.value } | |
} | |
} | |
extension JSONDecoder { | |
func decodeArrayWithOptionalElements<T>(_ type: [T].Type, from data: Data) throws -> [T] where T : Decodable { | |
return (try decode([DecodableContainer<T>].self, from: data)).compactMap { $0.value } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment