我正在使用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。我还看不到我所缺少的东西。你能看出这个映射有什么问题吗?
由于
答案 0 :(得分:11)
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
}
}
我希望这对你有所帮助。
答案 1 :(得分:7)
您可以创建一个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"))
}
答案 2 :(得分:1)
使用SwiftDate库将RodolfoAntonici答案重写为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
}
}
答案 3 :(得分:0)
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
}
}