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
Save XML data to UserDefaults #22
Comments
@meshileya thank you for choosing this library. 😃 To use If you don't want to use func saveListXMLData(podList: [RSSFeeds]) {
let podData = try? XMLSerialization.data(withXMLObject: podList.toXML())
UserDefaults.standard.set(podData, forKey: "podData")
UserDefaults.standard.synchronize()
} Just remember to convert the data back to your type, when you are retrieving them like: func getListXMLData() -> [RSSFeeds]? {
guard let data = UserDefaults.standard.data(forKey: "podData") else {
return nil
}
let xml = try? XMLSerialization.xmlObject(with: data)
return XMLMapper<RSSFeeds>().mapArray(XMLObject: xml)
} On the other hand, you can just save your XML string to the UserDefaults like: func saveListXMLString(podList: [RSSFeeds]) {
UserDefaults.standard.set(podList.toXMLString(), forKey: "podData")
UserDefaults.standard.synchronize()
} And map it back to your object type, when you are retrieving it like: func getListXMLString() -> [RSSFeeds]? {
guard let xmlString = UserDefaults.standard.string(forKey: "podData") else {
return nil
}
return XMLMapper<RSSFeeds>().mapArray(XMLString: xmlString)
} All of the above ways will have the same outcome. Hope this helps. |
@meshileya the Having said that, if your XML looks like this: <root>
<itunes:category text="Life & Love"/>
</root> You can use nested attribute mapping to map class Root: XMLMappable {
var nodeName: String!
var itunesCategoryText: String?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
itunesCategoryText <- map.attributes["itunes:category.text"]
}
} Hope this helps. |
Oh yeah, thanks @gcharita I really appreciate. But I think I have got into a slight issue now. Whenever I try to save the data using UserDefaults, it actually saves well but if I want to retrieve the data, the below is the error I do get. The only weird part is, it actually works without having to save in UserDefaults but once that is done, then I get the error below. I really appreciate your effort bro :( Error Domain=NSXMLParserErrorDomain Code=68 "(null)" UserInfo={NSXMLParserErrorColumn=56, NSXMLParserErrorLineNumber=1, NSXMLParserErrorMessage=xmlParseEntityRef: no name |
@meshileya what method do you use to save to the UserDefaults? If you convert your XML to |
This is actually the way I am doing the write and read
It only happens when I have |
@meshileya there is an interesting bug that I just figured it out. Sorry for the inconvenience. Take a look at #23 for a temporary fix. |
I am closing this, since the question was answered and the #23 is not related to this. @meshileya thank you for asking. For questions regarding XMLMapper feel free to use Stack Overflow. |
Hello @gcharita , I will so much appreciate your insight as regards this. I am currently working on an XML with several categories. e.g.
but, whenever I use category <- map.attributes["itunes:category.text"] , it returns null. but it works perfectly well for XML with just one I will like to ask if there is anything I am doing wrong, pls. |
@meshileya unfortunately, you have to create a new class Root: XMLMappable {
var nodeName: String!
var itunesCategories: [ITunesCategory]?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
itunesCategories <- map["itunes:category"]
}
}
class ITunesCategory: XMLMappable {
var nodeName: String!
var text: String?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
text <- map.attributes["text"]
}
} Don't worry if you receive only one |
You are really one of the genii of our time |
Hello @gcharita , thanks for this great library. It is well appreciated.
I will like to ask how best I can store data to my UserDefaults.
I have already tried
but i am getting the
unrecognized selector sent to instance 0x600000c23040 error.
Any insight to getting this resolved will be well appreciated.
Regards,
The text was updated successfully, but these errors were encountered: