Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Foundation
/// Allows a property to fail decoding, defaulting to nil instead of surfacing the error.
@propertyWrapper
struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable {
var wrappedValue: Wrapped?
init() {
self.wrappedValue = nil
}
init(wrappedValue: Wrapped?) {
self.wrappedValue = wrappedValue
}
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.wrappedValue = try? container.decode(Wrapped.self)
}
}
extension AllowDecodeFailure: Equatable where Wrapped: Equatable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.