我正在使用ObjectMapper将json投射到物体中。我的问题是没有正确映射NSDate属性。这是json:
{
"Id":4775,
"Cor":{
"Id":2,
"Nome":"Amarelo",
"HTMLCode":"FFFB00"
},
"Data":"2016-07-25T09:35:00",
"Texto":"test test test",
"Kilometro":547.0
}
这是我可以上映的课程
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())
}
}
问题是date属性始终是1970-01-01。我还看不到我所缺少的东西。你能看出这个映射有什么问题吗?
谢谢
ObjectMapper没有正确地从String
转换为NSDate
你必须做一个像这样的解决方法来指定它需要从NSDate
转换的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
}
}
我希望这对你有帮助。
您可以创建一个TransformType类,允许将dateFormat作为参数:
// DateFormatTransform.swift
import Foundation
import ObjectMapper
public class DateFormatTransform: TransformType {
public typealias Object = NSDate
public typealias JSON = String
var dateFormat = NSDateFormatter(dateFormat: "yyyy-MM-dd HH:mm:ss")
convenience init(dateFormat: String) {
self.init()
self.dateFormat = NSDateFormatter(dateFormat: dateFormat)
}
public func transformFromJSON(value: AnyObject?) -> Object? {
if let dateString = value as? String {
return self.dateFormat.dateFromString(dateString)
}
return nil
}
public func transformToJSON(value: NSDate?) -> JSON? {
if let date = value {
return self.dateFormat.stringFromDate(date)
}
return nil
}
}
并像这样使用它:
func mapping(map: Map) {
id <- map["Id"]
color <- map["Cor"]
text <- map["Texto"]
kilometer <- map["Kilometro"]
date <- (map["Data"], DateFormatTransform(dateFormat: "yyyy-MM-dd"))
}
RodolfoAntonici使用SwiftDate库回答了Swift 4的重写
import SwiftDate
import Foundation
import ObjectMapper
public class DateFormatTransform: TransformType {
public typealias Object = Date
public typealias JSON = String
public func transformFromJSON(_ value: Any?) -> Object? {
if let dateString = value as? String {
return dateString.toDate()?.date
}
return nil
}
public func transformToJSON(_ value: Date?) -> JSON? {
if let date = value {
return date.toString()
}
return nil
}
}
import Foundation
import ObjectMapper
public class DateFormatTransform: TransformType {
public typealias Object = Date
public typealias JSON = Double
var dateFormat = DateFormatter()
convenience init(_ format: String) {
self.init()
self.dateFormat.dateFormat = format
}
open func transformFromJSON(_ value: Any?) -> Date? {
if let timeInt = value as? Double {
return Date(timeIntervalSince1970: TimeInterval(timeInt))
}
if let timeStr = value as? String {
return self.dateFormat.date(from: timeStr)
}
return nil
}
open func transformToJSON(_ value: Date?) -> Double? {
if let date = value {
return Double(date.timeIntervalSince1970)
}
return nil
}
}