gpt4 book ai didi

ios - 快速解析 JSON 数组,对其进行排序并找到重叠的日期

转载 作者:行者123 更新时间:2023-12-01 16:06:08 25 4
gpt4 key购买 nike

我如何解析这个?数组未命名,对象未命名?我还需要对其进行排序并找到事件之间的重叠(识别冲突),如果你们在那里也有任何建议,这对我来说将是一个巨大的帮助。

[{"title": "Evening Picnic", "start": "November 10, 2018 6:00 PM", "end": "November 10, 2018 7:00 PM"}, {"title": "Nap Break", "start": "November 8, 2018 12:56 PM", "end": "November 8, 2018 1:30 PM"}, {"title": "Football Game", "start": "November 3, 2018 6:14 PM", "end": "November 3, 2018 9:44 PM"}, {"title": "Evening Cookout with Friends", "start": "November 6, 2018 5:00 PM", "end": "November 6, 2018 10:00 PM"}, {"title": "Roller Derby", "start": "November 7, 2018 12:00 PM", "end": "November 7, 2018 2:30 PM"}, {"title": "Basketball Game", "start": "November 8, 2018 7:30 PM", "end": "November 8, 2018 10:30 PM"}, {"title": "Local Pub with Friends", "start": "November 1, 2018 7:33 PM", "end": "November 1, 2018 11:00 PM"}, {"title": "Dentist Appointment", "start": "November 10, 2018 1:45 PM", "end": "November 10, 2018 2:30 PM"}, {"title": "Volleyball With Friends", "start": "November 6, 2018 12:00 PM", "end": "November 6, 2018 1:26 PM"}, {"title": "Free Donuts", "start": "November 9, 2018 3:00 PM", "end": "November 9, 2018 4:00 PM"}, {"title": "TV Show Marathon", "start": "November 9, 2018 4:30 PM", "end": "November 9, 2018 9:00 PM"}, {"title": "Boba Tea Grand Opening", "start": "November 6, 2018 1:27 PM", "end": "November 6, 2018 2:00 PM"}, {"title": "Lunch with Friends", "start": "November 8, 2018 11:30 AM", "end": "November 8, 2018 12:59 PM"}, {"title": "SF Coffee Festival","start": "November 6, 2018 6:00 PM","end": "November 6, 2018 9:00 PM"}, {"title": "Beer with Friends", "start": "November 9, 2018 8:00 PM", "end": "November 9, 2018 9:30 PM"}, {"title": "Yoga", "start": "November 1, 2018 6:00 PM", "end": "November 1, 2018 7:33 PM"}, {"title": "Rock Concert", "start": "November 7, 2018 6:30 PM", "end": "November 7, 2018 11:00 PM"}, {"title": "Lunch Meeting", "start": "November 9, 2018 12:30 PM", "end": "November 9, 2018 2:30 PM"}, {"title": "Bicycling with Friends", "start": "November 1, 2018 6:00 AM", "end": "November 1, 2018 9:30 AM"}, {"title": "Birthday Party", "start": "November 10, 2018 12:30 PM", "end": "November 10, 2018 8:30 PM"}, {"title": "Football Tailgate with John", "start": "November 3, 2018 6:14 PM", "end": "November 3, 2018 9:44 PM"}]   

我是 swift 的新手,我想知道如何处理这个问题

最佳答案

考虑到这是您之前的后续问题 post .您可以使用 Swift DateInterval用于创建 DateInverval 的初始化程序与您的 startend日期并检查它们是否相互交叉:

extension Event {
var interval: DateInterval { .init(start: start, end: end) }
func intersects(with event: Event) -> Bool { interval.intersects(event.interval) }
}

要检查冲突事件,您需要使事件符合 Equatable 并过滤与它们相交但不是同一事件的事件:
struct Event: Codable, Equatable {
let title: String
let start: Date
let end: Date
}
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .monthDayYearTime
do {
let events = try decoder.decode([Event].self, from: Data(json.utf8))
print(events.sorted())
let conflictingEvents: [(Event, Event)] = events.compactMap {
for event in events where event != $0 {
if event.intersects(with: $0) && $0.end != event.start && event.end != $0.start { return ($0, event) }
}
return nil
}
print(events.count) // "21\n"
print(conflictingEvents.count) // "11\n"
} catch {
print(error)
}

关于ios - 快速解析 JSON 数组,对其进行排序并找到重叠的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60142054/

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