Удалите SwiftyJSON и используйте Decodable
для анализа JSON в структуре.Разбор дат очень настраиваемый.Вы можете добавить свою собственную логику, которая извлекает год и месяц из строки даты и создавать экземпляр Date
.
struct HistoricalData: Decodable {
let date: Date
let open, low, high : String
let close, adjustedClose, volume : Double
}
...
let jsonString = """
[{"date":"2017-09-01","open":"61.9300","high":"63.03","low":"61.4400","close":63.03,"adjusted_close":61.6402,"volume":0},
{"date":"2017-10-02","open":"63.3400","high":"64.5300","low":"63.3400","close":64.39,"adjusted_close":62.9703,"volume":0},
{"date":"2017-11-01","open":"64.4400","high":"66.35","low":"64.0600","close":66.35,"adjusted_close":64.8872,"volume":0},
{"date":"2017-12-01","open":"66.2100","high":"67.3500","low":"65.7700","close":66.7,"adjusted_close":65.5322,"volume":0},
{"date":"2018-01-02","open":"67.2500","high":"71.4800","low":"67.2500","close":70.24,"adjusted_close":69.0102,"volume":0},
{"date":"2018-02-01","open":"70.2400","high":"70.2400","low":"64.4000","close":67.63,"adjusted_close":66.4458,"volume":0},
{"date":"2018-12-03","open":"69.5700","high":"69.5700","low":"58.1700","close":62.08,"adjusted_close":62.08,"volume":0}]
"""
let data = Data(jsonString.utf8)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .custom { decoder -> Date in
let container = try decoder.singleValueContainer()
let dateStr = try container.decode(String.self)
let components = dateStr.components(separatedBy: "-")
guard components.count > 2, let year = Int(components[0]), let month = Int(components[1]) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date string") }
return Calendar.current.date(from: DateComponents(year: year, month: month))!
}
do {
let result = try decoder.decode([HistoricalData].self, from: data)
print(result)
} catch { print(error) }
В качестве альтернативы вы можете декодировать строку в формат yyyy-MM
, однако вам придетсянаписать инициализатор и добавить CodingKeys
struct HistoricalData: Decodable {
let date: String
let open, low, high : String
let close, adjustedClose, volume : Double
private enum CodingKeys : String, CodingKey {
case date, open, low, high, close, adjustedClose, volume
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let dateString = try container.decode(String.self, forKey: .date)
guard let secondDashRange = dateString.range(of: "-", options: .backwards) else {
throw DecodingError.dataCorruptedError(forKey: .date, in: container, debugDescription: "Invalid date string")
}
date = String(dateString[..<secondDashRange.lowerBound])
open = try container.decode(String.self, forKey: .open)
low = try container.decode(String.self, forKey: .low)
high = try container.decode(String.self, forKey: .high)
close = try container.decode(Double.self, forKey: .close)
adjustedClose = try container.decode(Double.self, forKey: .adjustedClose)
volume = try container.decode(Double.self, forKey: .volume)
}
}
let data = Data(jsonString.utf8)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let result = try decoder.decode([HistoricalData].self, from: data)
print(result)
} catch { print(error) }