CoreOffice / XMLCoder

Easy XML parsing using Codable protocols in Swift
https://coreoffice.github.io/XMLCoder/
MIT License
801 stars 112 forks source link

<element /> issue? #103

Closed pj4533 closed 5 years ago

pj4533 commented 5 years ago

This might be user error, or might be related to https://github.com/MaxDesiatov/XMLCoder/pull/101 not really sure. I am trying to decode:

<?xml version="1.0" encoding="UTF-8"?>
<compendium>
  <foobar>
    <foo>Foo string value</foo>
    <nested>
      <name>Name</name>
      <text>Bar string value</text>
    </nested>
    <nested>
      <name>Name</name>
      <text>Bar string value</text>
      <text />
      <text>Bar string value</text>
    </nested>
  </foobar>
</compendium>

My structs:

           struct NestedObject : Codable {
                var name : String?
                var text : [String]?
            }

            struct Foobar : Codable {
                var foo : String?
                var nested : [NestedObject]?
            }

            struct Compendium: Codable  {
                var foobars: [Foobar]
                enum CodingKeys: String, CodingKey {
                    case foobars = "foobar"
                }
            }

The empty text in the last nested object fails to decode. I think cause the String inside the array isn't optional, so when not there, the decoder freaks out? Not sure...any help would be appreciated!

pj4533 commented 5 years ago

Derp.....nevermind! I answered my own question.....just needed to make the array element optional!