This should work:
class ModalData
{
var id: String
var booking_date: String
init(id: String, booking_date: String)
{
self.id = id
self.booking_date = booking_date
}
}
var strDate = "2019-10-14, 2019-07-30"
var strID = "162670, 127097"
let dateArray = strDate.split(",").map { $0.trimmingCharacters(in: .whitespaces) }
let idArray = strID.split(",").map { $0.trimmingCharacters(in: .whitespaces) }
var modalDataArray = [ModalData]()
for i in 0..<max(dateArray.count, idArray.count)
{
modalDataArray.append(ModalData(id: idArray[i], booking_date: dateArray[i]))
print(modalDataArray.last!)
}
What this does is:
- Turn the string of dates into an array of dates (
dateArray
).
- Turn the string of ids into an array of ids (
idArray
).
(Both splitting by ,
and removing whiteSpaces around the string)
Then check the max amount in both arrays, just to be safe.
Using this, we can loop through the array and create ModalData
objects.