I'm using SWXMLHash and have written an extension
on NSDate
for XMLElementDeserializable.
I've followed how the basic types are extended at the end of this file.
What I have looks like this:
import Foundation
import SWXMLHash
struct BlogPost: XMLIndexerDeserializable {
let date: NSDate
static func deserialize(blogPost: XMLIndexer) throws -> BlogPost {
return try BlogPost(
date: blogPost["date"].value()
)
}
}
extension NSDate: XMLElementDeserializable {
/**
Attempts to deserialize XML element content to an NSDate
- element: the XMLElement to be deserialized
- throws: an XMLDeserializationError.TypeConversionFailed if the element cannot be deserialized
- returns: the deserialized NSDate value formatted as "EEE, dd MMM yyyy HH:mm:ss zzz"
*/
public static func deserialize(element: XMLElement) throws -> NSDate {
guard let dateAsString = element.text else {
throw XMLDeserializationError.NodeHasNoValue
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let date = dateFormatter.dateFromString(dateAsString)
guard let validDate = date else {
throw XMLDeserializationError.TypeConversionFailed(type: "Date", element: element)
}
return validDate
}
}
However, I'm getting an error that says:
Method 'deserialize' in non-final class 'NSDate' must return 'Self' to conform to protocol 'XMLElementDeserializable'
I've looked around S.O. for other answers to the same error and I haven't gleaned much information from them.
Any suggestions would be much appreciated. Thanks!