I am using this library, https://github.com/gcharita/XMLMapper.
It contains protocol,
public protocol XMLBaseMappable {
var nodeName: String! { get set }
}
I would like to make this nodeName
optional for my struct/classes implementing it, like
public protocol CustomXMLBaseMappable {
var nodeName: String? { return "default" }
//I don't want struct/classes using it, implements `nodeName`.
}
Any suggestions will be helpful.
The existence of this property in XMLBaseMappable
protocol is crucial in order to function correctly the whole library.
Having said that, you can't omit the implementation of this property in your structs and classes but you can "hide" it in a super class. Using this:
class BasicXMLMappable: XMLMappable {
var nodeName: String!
required init(map: XMLMap) {
}
func mapping(map: XMLMap) {
}
}
You can have XMLMappable
objects that extends BasicXMLMappable
and they don't have to implement nodeName
property:
class TestBasicXMLMappable: BasicXMLMappable {
// Your custom properties
required init(map: XMLMap) {
super.init(map: map)
}
override func mapping(map: XMLMap) {
// Map your custom properties
}
}
Edit:
As of version 1.5.1 you can use XMLStaticMappable
for implementing XMLMapper
in an extension. For example:
class CustomClass {
var property: String?
}
extension CustomClass: XMLStaticMappable {
var nodeName: String! {
get {
return "default"
}
set(newValue) {
}
}
static func objectForMapping(map: XMLMap) -> XMLBaseMappable? {
// Initialize CustomClass somehow
return CustomClass()
}
func mapping(map: XMLMap) {
property <- map["property"]
}
}
Hope this helps.