How to make working time from the array?

At the entrance there is an array of objects [Time] where
struct Time {
id: Int
from: String
to: String
}
id - 0-6 - mon-Sun
Need to bring the array to the view work schedule

Example:
[Time(0, 09:00, 12:00), Time(1, 09:00, 12:00), Time(0,13:00,18:00), Time(1, 13:00, 18:00), Time(2, 09:00, 12:00), Time(2, 13:00, 18:00), Time(4, 09:00, 17:00)]
The result:
Mon-Wed 09:00-12:00 13:00-18:00
Fri 09:00-17:00

What we have now (does not always work correctly)
private static var dayArr = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

public var openingHoursDict: [String: String] {
var tempArr = [String]()
if let arr = openingHours {
for ind in 0..<7 {
var time = ""
arr.forEach({ openingHour in
if openingHour.day == ind {
if let from = openingHour.from, let to = openingHour.to {
time += from + "-" + to + " "
}
}
})
tempArr.append(time)
}
}
var tempDict = [[Int]: String]()
for ind in 0..<7 {
let value = tempArr[ind]
var key = [Int]()
for indVal in tempArr.enumerated() where indVal.element == value {
key.append(indVal.offset)
}
key.sort()
if !value.isEmpty {
tempDict.updateValue(value, forKey: key)
}
}
var str = ""
var strDict = [String: String]()
for keyArray in tempDict.keys.enumerated() {
for ind in keyArray.element.enumerated() {
if ind.offset == 0 || ind.element != keyArray.element[ind.offset - 1] + 1 {
str += "\(PoiContact.dayArr[ind.element])-"
}
if ind.offset == keyArray.element.count - 1 {
str += PoiContact.dayArr[ind.element]
} else if keyArray.element[ind.offset + 1] != ind.element + 1 {
str += "\(PoiContact.dayArr[ind.element]), "
}
}
// str = str.replacingOccurrences(of: "/([A-Z][a-z]+)-\\1/", with: "\\1")
strDict.updateValue(tempDict[keyArray.element] ?? "", forKey: str)
}
return strDict
}
April 7th 20 at 15:26
1 answer
April 7th 20 at 15:28
Try the simpler:
Sort the array by id, save, start the same days and go through it in order. In each new iteration of the compare time. If the schedule is changed, the output string over time, and update the beginning of the same days.

Your solution looks difficult.

And I think that it would be logical lunchtime as a separate field to enter. So it is easier to work and the id is unique:
struct Time {
 var id: Int
 var from: String
 var to: String
 var dinnerBreakFrom: String
 var dinnerBreakTo: String
}

Or else the entity to add, which will combine the two pieces of Timeif you don't want something to rewrite.

Find more questions by tags iOSSwift