I'm using ObjectMapper to cast json into objects. My problem is that the NSDate property is not being mapped correctly. Here is the json:
{
"Id":4775,
"Cor":{
"Id":2,
"Nome":"Amarelo",
"HTMLCode":"FFFB00"
},
"Data":"2016-07-25T09:35:00",
"Texto":"test test test",
"Kilometro":547.0
}
And here is my mappable class
class RoadWarning : Mappable {
var id: Int?
var color: RoadWarningColor?
var date: NSDate?
var text: String?
var kilometer: Float?
required init?(_ map: Map){
}
func mapping(map: Map) {
id <- map["Id"]
color <- map["Cor"]
text <- map["Texto"]
kilometer <- map["Kilometro"]
date <- (map["Data"], DateTransform())
}
}
The problem is that the date property is always 1970-01-01. I can't see yet what I am missing. Can you see what is wrong in this mapping?
Thanks
ObjectMapper not convert from String
to NSDate
properly you have to make a workaround like this to specify the type of NSDate
format it need to convert from the String
:
func mapping(map: Map) {
id <- map["Id"]
color <- map["Cor"]
text <- map["Texto"]
kilometer <- map["Kilometro"]
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let dateString = map["Data"].currentValue as? String, let _date = dateFormatter.dateFromString(dateString) {
date = _date
}
}
I hope this help you.