我已经编写了以下代码,但是我得到了nil作为timestamp,而且我假设当timestamp是iso-8601格式时,“date”不是正确的类型:
Swift.DecodingError.Context文件(codingPath:[\u JSONKey(stringValue:”索引
0“,intValue:0),编码键(stringValue:“timestamp”,intValue:
nil)],debugDescription:“应解码Double,但发现
雨燕4
import UIKit
enum Type : String, Codable {
case organizational, planning
}
// structure from json file
struct News: Codable{
let type: Type
let timestamp: Date //comes in json with ISO-8601-format
let title: String
let message: String
enum CodingKeys: String, CodingKey { case type, timestamp, title, message}
let dateFormatter : DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yy HH:mm:ss" // change format ISO-8601 to dd/MM/yy HH:mm:ss
return formatter
}()
var dateString : String {
return dateFormatter.string(from:timestamp) // take timestamp variable of type date and make it a string -> lable.text
}
}