当我试图用可解码的生日来解析我的json时,来自零 . 我应该使用任何建议或代码示例的日期格式 . 我的日期格式包括时区 .
我的问题是生日来自零 . 如何用可解码的方式解析生日?
My json :
{
"id": 1,
"name": "fatih",
"birddate": "2018-09-19T11:36:00.4033163+03:00",
"total": 0.9,
"isTest": false
}
here is my struct :
struct TestDTO : Decodable {
var id:Int?
var name : String?
var birtdate : Date?
var total : Double?
var isTest : Bool?
}
RestClientServiceTest().CallRestService(matching: cmd, completion: { (data) in
do{
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let stories = try decoder.decode(TestDTO.self, from: data!)
print(data)
}catch let error{
print("Json Parse Error : \(error)")
}
})
2 回答
首先在
struct
中使用的名称是错误的birddate
- 拼写错误 .秒不要将其设置为
Date
它是String
.获取数据为
String
并在使用时使用NSDateFormatter
.您可以在SO上搜索
NSDateFormatter
.所以,在操场上玩一玩......
打印
2018-09-19 08:36:00 +0000
所以进一步迈出了一步......
打印...
你可能会发现Easy Skeezy Date Formatting for Swift有些用处