gpt4 book ai didi

ios - 如何快速检查时间重叠?

转载 作者:行者123 更新时间:2023-11-28 05:48:49 26 4
gpt4 key购买 nike

我的 View 中有 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

我使用这个公式将这个时间转换为十进制:

(分钟 + (小时*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)

}
}

请告诉我这样做的想法?

最佳答案

您需要检查是否有两个 Date范围重叠。这可以通过检查是否有 ClosedRange<Date> 来实现。重叠另一个

let isOverlapping = (startDate1...endDate1).overlaps(startDate2...endDate2)

但是对于您的情况,如果这次时间(在您的情况下为 Int 索引)与数组中的任何其他时间重叠,您不想将其附加到数组中,因此在您附加此新时间之前检查是否与某些时间重叠其他时间。如果不追加它,如果追加它,做任何你想做的事。

let array: [[String: Int]] = ...
let newStartIndex: Int = ...
let newEndIndex: Int = ...
let isOverlapping = array.allSatisfy { $0["startTime"] <= newEndIndex && newStartIndex <= $0["stopTime"]}

关于ios - 如何快速检查时间重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53700005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com