Last active
September 1, 2021 16:57
-
-
Save denandreychuk/6f174b46a62baf3bd7f7f3dd7c554cc2 to your computer and use it in GitHub Desktop.
Опциональное декодирование элементов массива с помощью Decodable | https://t.me/BlogSwift
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
| // СВИФТЕР | Блог про Swift | t.me/BlogSwift | |
| struct FailableDecodable<Base : Decodable> : Decodable { | |
| let base: Base? | |
| init(from decoder: Decoder) throws { | |
| let container = try decoder.singleValueContainer() | |
| self.base = try? container.decode(Base.self) | |
| } | |
| } | |
| // Опциональное декодирования элементов массива | |
| self.songs = try container | |
| .decode([FailableDecodable<Song>].self, forKey: .songs) | |
| .compactMap(\.base) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment