I am parsing json from server. I am getting 4 values in json so I created model class
class PriceData: Mappable {
var buy: Double?
var sell: Double?
var spot_price: NSNumber?
var timestamp: String?
var timesStampDt: Date?
required init?(map: Map) {
//
}
func mapping(map: Map) {
buy <- map["buy"]
sell <- map["sell"]
spot_price <- map["spot_price"]
timestamp <- map["timestamp"]
print(String(describing: GlobalMethods.dateFormat(dt: timestamp!)))
timesStampDt <- map[String(describing: GlobalMethods.dateFormat(dt:
timestamp!))]
}
}
I am geting timestamp as string type but I need to covert into date while parsing so I converted timeStamp to date by using this method and
static func dateFormat(dt: String) -> Date{
let formatter = Foundation.DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date1 = formatter.date(from: dt)
print("date:\(String(describing: date1))")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let resultTime = formatter.date(from: dt)
return resultTime!
}
but I when passing the converted date to "timesStampDt" in mapping func
the value of timesStampDt is nil.
You probably want to use Transform method from object mapper. Check DateTransform class and use it like follows.
timesStampDt <- (map["timestamp"], DateTransform())
Tis will transform your timestamp to Date. You can further study the transform class and make any Object Transform for yourself, This is very flexible