Created
January 24, 2020 11:20
-
-
Save ConfusedVorlon/276bd7ac6c41a99ea0514a34ee9afc3d to your computer and use it in GitHub Desktop.
Add Codable support to @published
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
import Foundation | |
import SwiftUI | |
extension Published:Decodable where Value:Decodable { | |
public init(from decoder: Decoder) throws { | |
let decoded = try Value(from:decoder) | |
self = Published(initialValue:decoded) | |
} | |
} | |
extension Published:Encodable where Value:Decodable { | |
public func encode(to encoder: Encoder) throws { | |
let mirror = Mirror(reflecting: self) | |
if let valueChild = mirror.children.first(where: { $0.label == "value" | |
}) { | |
if let value = valueChild.value as? Encodable { | |
do { | |
try value.encode(to: encoder) | |
return | |
} catch let error { | |
assertionFailure("Failed encoding: \(self) - \(error)") | |
} | |
} | |
else { | |
assertionFailure("Decodable Value not decodable. Odd \(self)") | |
} | |
} | |
else { | |
assertionFailure("Mirror Mirror on the wall - why no value y'all : \(self)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An approach that is working instead of protocols is using enums: