Created
July 9, 2020 11:19
-
-
Save MojtabaHs/4ce6e354a37b8d89f17ec3731b4d2d87 to your computer and use it in GitHub Desktop.
A property wrapper for arrays with invalid decodable elements.
This file contains 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
@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