问题描述
我该如何解析?数组未命名,对象未命名?如果你们也有任何建议,我还需要对事件进行排序并查找事件之间的重叠(确定冲突),这对我来说将是巨大的帮助.
How do I parse this? The array is unnamed, the objects are unnamed? I also need to sort it and find overlaps(identify conflicts) between events IF you guys have any advice there too it will be a huge help for me.
[{"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"}]
我是新手,我想知道如何处理
I'm new to swift and I'm wondering how to deal with this
推荐答案
考虑到这是您上一个帖子中的跟进问题一个>.您可以使用Swift DateInterval 初始化程序来创建DateInverval
和您的start
end
日期并检查它们是否相交:
Considering this is a follow up question from your previous post. You can use Swift DateInterval initialiser to create a DateInverval
with your start
and end
dates and check if they intersects with each other:
extension Event {
var interval: DateInterval { .init(start: start, end: end) }
func intersects(with event: Event) -> Bool { interval.intersects(event.interval) }
}
要检查是否存在冲突事件,您需要使事件符合Equatable并过滤与它们相交但不是同一事件的事件:
To check for conflicting events you would need to make your Event conform to Equatable and filter the ones that intersects with them but are not the same event:
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)
}
这篇关于快速解析JSON数组,对其进行排序并查找重叠的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!