So I have JSON data that's in an array (0 is first day of month up until last day of month).
How do I do a loop to go through the whole array and store the 3 times of the day each in their own array (I wish to use these times for notifications later)
The dayForArray variable is just the current day - 1 so that it matches the number in the JSON array.
func parseJSON(_ timesData: Data) -> TimesModel? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(TimesData.self, from: timesData)
let time1 = decodedData.data[dayForArray].timings.Time1
let time2 = decodedData.data[dayForArray].timings.Time2
let time3 = decodedData.data[dayForArray].timings.Time3
let time = TimeModel(time1: time1, time2: time2, time3: time3)
return times
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
What you need to do is to use map
to create an array of TimeModel
objects from your json array
let times = decodedData.data.map {
TimeModel(time1: $0.timings.Time1,
time2: $0.timings.Time2,
time3: $0.timings.Time3)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.