LoginSignup
4
5

More than 5 years have passed since last update.

ObjectMapperでの日付マッピング方法

Posted at

ObjectMapperスゲー!となってる今日このごろですが、日付のマッピングはどうするんだろうと思って少し調べたら
以下の書き方で日付のマッピングができるとわかりました。

class dateItems: Object,Mappable {
    dynamic var day:NSDate?

    /// ObjectMapper
    required convenience init?(_ map: Map) {
        self.init()
        mapping(map)
    }

    func mapping(map: Map) {
        day <- (map["day"],CustomDateFormatTransform(formatString: "yyyy-MM-dd"))
    }
}

今回マッピングしたい日付は以下のようなものです。

2016-07-01 00:00:00 +0000

マッピングするために日付のフォーマットの書き方どうすればいいんだっけ?となり、調べてたら以下の記事を見つけました。

dateformat

これを参考にすると以下の書き方になりました。

class dateItems: Object,Mappable {
    dynamic var day:NSDate?

    /// ObjectMapper
    required convenience init?(_ map: Map) {
        self.init()
        mapping(map)
    }

    func mapping(map: Map) {
        day <- (map["day"],CustomDateFormatTransform(formatString: "yyyy-MM-dd'T'HH:mm:ssxxx"))
    }
}

無事マッピングされて一安心!

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5