Skip to content

Instantly share code, notes, and snippets.

@MojtabaHs
Created July 9, 2020 11:19
Show Gist options
  • Save MojtabaHs/4ce6e354a37b8d89f17ec3731b4d2d87 to your computer and use it in GitHub Desktop.
Save MojtabaHs/4ce6e354a37b8d89f17ec3731b4d2d87 to your computer and use it in GitHub Desktop.
A property wrapper for arrays with invalid decodable elements.
@propertyWrapper
struct Fallible<Value: Decodable>: Decodable {
var wrappedValue: [Value] = []
private struct _None: Decodable {}
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
if let decoded = try? container.decode(Value.self) {
wrappedValue.append(decoded)
}
else {
// item is silently ignored.
try? container.decode(_None.self)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment