我正在使用SWXMLHash并extension
为NSDate
XMLElementDeserializable.
我已经按照这个文件末尾的基本类型是如何扩展的。
我所拥有的看起来像这样:
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
}
}
但是,我收到一条错误消息:
非最终类“NSDate”中的方法“反序列化”必须返回“Self”以符合协议“XMLElementDeserializable”
我环顾四周寻找相同错误的其他答案,但我没有从他们那里收集到太多信息。
任何建议将不胜感激。谢谢!