-
Notifications
You must be signed in to change notification settings - Fork 21
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
Map to a bool based on the existence of a tag #15
Comments
@bhirt there is a way but is rather difficult. First of all, there is the do {
let readOptions: XMLSerialization.ReadingOptions = [
.collapseTextNodes,
.trimWhiteSpace,
.prefixedAttributes,
.alwaysNodeName
]
guard let xmlDict = try XMLSerialization.xmlObject(withString: xmlString, options: readOptions) as? [String: Any] else {
return
}
} catch {
print("Deserialization error: \(error.localizedDescription)")
} after that you can map the generated // Instead of TestFlag you should use your model class
let testFlag = XMLMapper<TestFlag>().map(XML: xmlDict) In your model class to deserialize the flag you can check if the tag exists in the func mapping(map: XMLMap) {
hasThing = map.XML.keys.contains("hasThing")
} To serialize the flag, unfortunately, you can't use the Bool type because will mapped like private class HasThing: XMLMappable {
var nodeName: String!
init() { }
required init?(map: XMLMap) { }
func mapping(map: XMLMap) { }
}
private var privateHasThing: HasThing?
var hasThing: Bool? {
didSet {
if hasThing == true {
privateHasThing = HasThing()
} else {
privateHasThing = nil
}
}
} Your model class in the end will look something like this: class TestFlag: XMLMappable {
private class HasThing: XMLMappable {
var nodeName: String!
init() { }
required init?(map: XMLMap) { }
func mapping(map: XMLMap) { }
}
var nodeName: String!
private var privateHasThing: HasThing?
var hasThing: Bool? {
didSet {
if hasThing == true {
privateHasThing = HasThing()
} else {
privateHasThing = nil
}
}
}
init() { }
required init?(map: XMLMap) { }
func mapping(map: XMLMap) {
privateHasThing <- map["hasThing"]
hasThing = map.XML.keys.contains("hasThing")
}
} This is not pretty but will work in both serialization and deserialization. Hope this workaround will help you. |
Thank you very much, I appreciate you help. |
I have an xml file that has an element that is a flag. For example, <hasThing/>. If that element exists I want to map it to true, otherwise it should be mapped to false. I didn't see anything in the readme. Is there a way to do this?
Thanks.
The text was updated successfully, but these errors were encountered: