Swift:将 NSDictionary 格式化为具有特定日期格式的 JSON

Swift: formatting NSDictionary as JSON with specific date format

Swift 中有没有办法为 NSDate 对象指定特定的日期格式?

例如,如果我有一个 NSDictionaryNSDate 个对象,并且想要这样 JSON:

{
  "something": [{
    "something_else": [{
      "data": {
        "total": 117,
        "updateTime": "2016-06-07T05:26:52-0300",
        "endTime": "2016-06-07T08:26:52-0300",
        "startTime": "2016-06-06T08:26:52-0300",
        "dayRangeBars": [{
          "belowRange": 36,
          "aboveRange": 5
        }]
      }
    }]
  }]
}

NSJSONSerialization.dataWithJSONObject是否有指定日期格式的方法?

您需要将自己的日期字符串化

示例:

var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
var dateString = formatter.stringFromDate(YourNSDateInstanceHERE)

我不这么认为。有一些图书馆可以做到这一点——Mantle is probably the most well-known one. There is also this: https://github.com/jagill/JAGPropertyConverter