Closed monibu91 closed 4 years ago
I have also tried something like:
enum BlockChild: Equatable {
case group(GroupModel)
case text(TextModel)
case image(ImageModel)
case diagram(DiagramModel)
case embed(EmbedModel)
}
extension BlockChild: Codable {
enum CodingKeys: String, CodingKey {
case group = "my:group"
case text = "my:text"
case image = "my:image"
case diagram = "my:diagram"
case embed = "my:embed"
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case let .group(value):
try container.encode(value, forKey: .group)
case let .text(value):
try container.encode(value, forKey: .text)
case let .image(value):
try container.encode(value, forKey: .image)
case let .diagram(value):
try container.encode(value, forKey: .diagram)
case let .embed(value):
try container.encode(value, forKey: .embed)
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let tempGroup = try? container.decode(GroupModel.self, forKey: .group){
self = .group(tempGroup)
}
else if let tempText = try? container.decode(TextModel.self, forKey: .text){
self = .text(tempText)
}
else if let tempImage = try? container.decode(ImageModel.self, forKey: .image){
self = .image(tempImage)
}
else if let tempDiagram = try? container.decode(DiagramModel.self, forKey: .diagram){
self = .diagram(tempDiagram)
}
else {
let tempEmbed = try container.decode(EmbedModel.self, forKey: .embed)
self = .embed(tempEmbed)
}
}
}
But this give me error:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [XMLKey(stringValue: "0", intValue: 0), XMLKey(stringValue: "0", intValue: 0)], debugDescription: "Expected to decode Dictionary<String, Any> but found ChoiceBox instead.", underlyingError: nil))
With the master branch my code works, but i was using cocoa-pods which i guess is an older version. So the fix for me was to add XMLCoder as Swift package pointing to master.
Sorry for the delay, just having a look at this. A new version of XMLCoder will be tagged soon, which will make the fix available on CocoaPods. Thanks for reporting the issue!
Closing as resolved in 0.10
I have this xml:
Which represents content on a page. The page has its properties and one group which is the root node of a tree structure. The nodes of the tree are "blocks". A block is like abstract class because there are no elements in the xml that are "block", but "group" , " text", "image", "diagram" and "embed" are blocks ( they have all the properties a block has (id, x, y, width, height, padding-left, padding-right, padding-top, padding-bottom). So my problem is that i don't find a way to express that group is an array of things that are some children of "block" structure. I have to encode and decode the xml. Here is what i did so far:
Do you have any idea how i can do it?