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)") | |
} | |
} | |
} |
I tried @StevenSorial 's solution, however it doesn't work if the type of a @Published
variable is any
protocol.
e.g.:
class Parent {
@Published var child: any Child
}
protocol Child: Codable { }
class CustomChild: Child {
var value: String
}
The error is:
Cannot automatically synthesize 'Decodable' because 'Published <any Child>' does not conform to 'Decodable'
Do you think this is possible?
The motivation for using a protocol instead of a superclass is because using a superclass requires all of the subclasses to implement the encoders and decoders.
An approach that is working instead of protocols is using enums:
class Parent {
@Published var child: Child
}
enum Child: Codable {
case custom(_ child: CustomChild)
}
class CustomChild: Codable {
var value: String
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Stevenmagdy , you are amazing! Your code worked perfectly! (The existing code kept crashing). Thank you!