Swift 传递不同日期的对象映射器

Swift Object Mapper passing a different date

我正在使用 ObjectMapper 将 json 放入对象中,但每次都将日期设置为 1970-01-01。我看不出我的问题是什么,因为我虽然 DateTransform 能够处理格式。

这是 class:

import Foundation
import ObjectMapper

class example :Mappable
{
    var ExampleDate: Date?

    required init?(map: Map){
    }

    //Mappable
    func mapping(map: Map){
        ExampleDate          <- (map["ReviewDate"], DateTransform())
    }

}

其中一个日期是这样的:

示例日期 = "2018-07-05T12:41:52.087+00:00"

谢谢!

尝试使用 DateFormatterTransform 代替:

import Foundation
import ObjectMapper

class example :Mappable
{
    var ExampleDate: Date?

    required init?(map: Map){
    }

    //Mappable
    func mapping(map: Map){
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZ"
        dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
        dateFormatter.locale = Locale(identifier: "en_US_POSIX")

        ExampleDate <- (map["ReviewDate"], DateFormatterTransform(dateFormatter: dateFormatter))
    }
}

一般来说,请尽量避免在变量名中使用首字母大写。这是推荐的方法,因此您可以轻松地将它们与类型区分开来。所以使用 exampleDate 而不是 ExampleDate