我的视图中有4个startTime datePicker和4个endTime datePicker . 我想检查时间是否重叠 .
Ex. slot 1 = 09:00 - 12:00
slot 2 = 08:00 - 11:00
slot 3 = 05:00 - 10:30
slot 4 = 11:00 - 16:00
我使用这个公式将这个时间转换为十进制:
(Minutes + (Hours*6))/10
现在我有小数时间,所以我尝试下面的代码:
for item in validationArray! {
let startIndexvalue:Int = item["startTime"]!
let stopIndexvalue:Int = item["stopTime"]!
print(startIndexvalue)
print(stopIndexvalue)
print(fastLinkStartTime)
if stopIndexvalue > startIndexvalue {
if startIndexvalue ... stopIndexvalue ~= fastLinkStartTime {
if startIndexvalue == 0 {
validationTimeDic["startTime"] = fastLinkStartTime
timeDic["startTime"] = hours + "0"
}
else {
showValidationAlert(title: NSLocalizedString("You can not select time in between existing time slot.", comment: ""), message: "")
DispatchQueue.main.async {
self.StartTime.text = "--:--"
}
validationTimeDic["startTime"] = 0
timeDic["startTime"] = "--:--"
}
validationArray?[currentRow] = validationTimeDic
self.currentObject!.validationData[currentSection.description] = validationArray
array?[currentRow] = timeDic
self.currentObject!.mondayToSundayData[currentSection.description] = array
print(self.currentObject!.validationData)
}
}
请给我任何想法吗?
2 回答
您可以为此用途扩展Range类,也可以在CGRect类上使用intersection .
// CGRect用法
// ClosedRange用法
你需要检查两个
Date
范围是否过度 . 这可以通过检查一个ClosedRange<Date>
是否与另一个重叠来完成但对于您的情况,您不希望将此时间(在您的情况下为
Int
index)附加到数组(如果它与数组中的任何其他时间重叠),因此在您附加此新时间之前检查是否与其他时间重叠 . 如果没有附加它,如果没有,做你想做的任何事情 .