Skip to content

Instantly share code, notes, and snippets.

@denandreychuk
Last active September 1, 2021 16:57
Show Gist options
  • Select an option

  • Save denandreychuk/6f174b46a62baf3bd7f7f3dd7c554cc2 to your computer and use it in GitHub Desktop.

Select an option

Save denandreychuk/6f174b46a62baf3bd7f7f3dd7c554cc2 to your computer and use it in GitHub Desktop.
Опциональное декодирование элементов массива с помощью Decodable | https://t.me/BlogSwift
// СВИФТЕР | Блог про 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