CoreOffice / XMLCoder

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

(Help using) How to decode this XML? #180

Closed thefredelement closed 4 years ago

thefredelement commented 4 years ago

Hi,

This is an awesome framework, thank you for making it. I have an XML that looks like this:

<Response>
  <status>success</status>
  <Things>
    <Thing>
      <ID>123456</ID>
    </Thing>
  </Things>
</Response>

And a Codable that looks like this:

struct ResponseThing: Codable {
  let ID: String
}

struct Response: Codable {

  let status: String
  let Things: [ResponseThing]
}

throws a missing key error... what am I missing?

thomasnordquist commented 4 years ago

let Things: [ResponseThing] would decode an array of Things not an array of Thing

To decode the given XML you would need something like

struct Response: Codable {
  let status: String
  let Things: ThingContainer
}

struct ThingContainer: Codable {
  let Thing: [ResponseThing]
}

struct ResponseThing: Codable {
  let ID: String
}

You might also want to take a look at CodingKeys, they will make your code much more readable.

thefredelement commented 4 years ago

As soon as I read that I got it, thank you!!!