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

This framework really works? keyNotFound #249

Open
Rufy86 opened this issue Sep 5, 2022 · 4 comments
Open

This framework really works? keyNotFound #249

Rufy86 opened this issue Sep 5, 2022 · 4 comments

Comments

@Rufy86
Copy link

Rufy86 commented Sep 5, 2022

I'm try to decode an XML returned by an API. the result is an error:

Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "ns2:body", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "ns2:body", intValue: nil)], debugDescription: "No attribute or element found for key CodingKeys(stringValue: "ns2:body", intValue: nil) ("ns2:body").", underlyingError: nil))

this is the struct
`struct AirportList:Codable {
let ns2:String

enum CodingKeys: String, CodingKey {
    case ns2 = "ns2:body"
}

}`
I've changed the key in model class, with any tag I need but the result is the same.

this framework really works?

@Joannis
Copy link
Member

Joannis commented Sep 5, 2022

This should work, and is in our unit tests. Have you set decoder.shouldProcessNamespaces = true? That causes the namespaces to be stripped.

@mflint
Copy link
Collaborator

mflint commented Sep 5, 2022

Hello @Rufy86,

In your code, ns2 is the namespace, which is different from the property name. If you don't care about namespaces, then the easiest solution might be:

struct AirportList: Codable {
  let body: String
}

// and to decode the XML...
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = true
let airportList = try decoder.decode(AirportList.self, from: data)

@Rufy86
Copy link
Author

Rufy86 commented Sep 7, 2022

thank you very much for the answer but I've still problems
this is a piece oaf my xml
<?xml version="1.0" encoding="utf-8" ?> <ns2:body xmlns:ns2="http://www.jaapp.it"> <airportlist> <hash>F12887A97073B98718174A8AFCDE5F65</hash> <changed>true</changed> <airports> <airport> <id>AAL_APT_0001</id><code>AAL</code> <name>Int</name> <citycode>AAL</citycode> <city>Aalborg</city> <countrycode>DK</countrycode> <country>Danimarca</country> <continent>Europa</continent> <latitude>57,09275891</latitude> <longitude>9,849243164</longitude> </airport> <airport> <id>AES_APT_0019</id><code>AES</code> <name>Aalesund Vigra</name> <citycode>AES</citycode> <city>Aalesund</city> <countrycode>NO</countrycode> <country>Norvegia</country> <continent>Europa</continent> <latitude>62,5625</latitude> <longitude>6,119699955</longitude> </airport> <airport> <id>AAR_APT_0001</id><code>AAR</code> <name>Aarhus Airport</name> <citycode>AAR</citycode> <city>Aarhus</city> <countrycode>DK</countrycode> <country>Danimarca</country> <continent>Europa</continent> <latitude>56,2999992</latitude> <longitude>10,6190004</longitude> </airport> </airports> </airportlist> </ns2:body>

Now I've no error from decoder but the objects are empty

this is my code:

AF.request("url_to_my_api").responseData { response in guard let airportList = response.data else { return } let decoder = XMLDecoder() decoder.shouldProcessNamespaces = true let note = try! decoder.decode(AirportList.self, from: airportList) print(note) }

@Joannis
Copy link
Member

Joannis commented Sep 13, 2022

@Rufy86 are you expecting the whole hierarchy of XML inside that String? Because if so, that's not how XMLCoder was designed. Each XML element or attribute is decoded into it's own codable key/value. So you'll need to model your Codable type to match the expectations in XML.

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

3 participants