Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thefredelement opened this issue May 11, 2020 · 2 comments
Closed

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

thefredelement opened this issue May 11, 2020 · 2 comments

Comments

@thefredelement
Copy link

thefredelement commented May 11, 2020

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
Copy link

thomasnordquist commented May 11, 2020

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants