Swift:如何在比较/拆分两个日期数组[date]之后创建一个日期数组数组[[date]]



简介

我正在创建一个日历应用程序,我需要管理一个日期数组,以便为UITableView提供数据。

上下文

数据模型的类型为[Date : [CalendarEvent]],其中CalendarEvent是一个NSManagedObject子类,我将其排序到根据事件相关日期属性分组的字典中。在我的UITableView中,该字典的每个键都有一个部分(我不会一次获取所有日历事件(。但是,我想添加更多的部分来显示带有事件的日期之间的日期间隔,使用类型为[[Date]]的数组

  • 澄清:图片显示我的日历应用程序存储了2个事件。2018-12-05和2018-12-09各一次,今天的日期是2018-12-01。在这种情况下,我想检索一个数组,如下所示:[ [2018-12-01, 2018-12-02, 2018-12-03, 2018-12-04], [2018-12-05], [2018-12-06, 2018-12-07, 2018-12-08], [2018-12-09], [2018-12-10] ],其中每个日期的类型当然是Date。(将产生4个部分(

问题

  • 如何对数组进行排序/拆分以满足格式[[Date]](在上面的"澄清"中解释(
    • 如果有一种更简单的方法可以达到同样的结果,那也将被视为一个答案

我的尝试

我缩小了比例,只显示必要的部分。

class CalendarViewController: UIViewController {
private let currentCalenar = Calendar.current
var events : [Date: [CalendarEvent]]? // Events is fetched from todays date and 10 days forward.
var sectionDates : [[Date]]?
func getDatesWithEvents() -> [Date]? {
if let keyArray = events?.keys {
var dateArray = Array(keyArray)
dateArray.sort { $0 < $1 }
return dateArray
}
return nil
}
func getSectionDatesArray() -> [[Date]]? { 
var sectionDatesArray : [[Date]] = []
var currentDate = currentCalendar.startOfDay(for: Date())
guard let endDate = currentCalendar.date(byAdding: .day, value: 9, to: currentDate), let datesWithEvent = getDatesWithEvents() else { return nil }
while currentDate < endDate {
if datesWithEvent.contains(currentDate) {
sectionDatesArray.append([currentDate])
sectionDatesArray.append([])
} else {
if !sectionDatesArray.isEmpty {
sectionDatesArray[sectionDatesArray.count - 1].append(currentDate)
} else {
sectionDatesArray.append([currentDate])
}
}
currentDate = currentCalendar.date(byAdding: .day, value: 1, to: currentDate)!
}
sectionDatesArray.removeAll { (sequence) -> Bool in
sequence.isEmpty
}
return sectionDatesArray
}

谢谢你阅读我的问题。

通常,在单个UITableView上使用多个数据源会带来坏运气。。您可能会考虑更改数据模型。

在您的示例中,我会考虑合并这两个数据源,使没有事件的日期的值为nil,因此您将其声明为var dates: [Date: [CalendarEvent]?]?

另一种方法——你可以切断这个字典,使用一个数组来存储类型(建议名称(为CalendarItem的对象——在那里你可以存储项目的Date[CalendarEvent]?

尽管如此,有很多方法可以做到这一点,我不打算涵盖所有方法。。。

如果你想坚持你的模型,这里有一个我做的函数。这可能不是最佳解决方案,有一些神奇的方法可以在4行中完成:(但现在开始吧-

var currentDate = Date()
// just made up dates
let datesWithEvents: [Date] = [
Calendar.current.date(byAdding: .day, value: 5, to: currentDate)!,
Calendar.current.date(byAdding: .day, value: 10, to: currentDate)!
]
let endDate = Calendar.current.date(byAdding: .day, value: 15, to: currentDate)!
var dates: [[Date]] = []
while currentDate < endDate{
if datesWithEvents.contains(currentDate){
dates.append([currentDate])
dates.append([])
}else{
if !dates.isEmpty{
dates[dates.count - 1].append(currentDate)
}else{
dates.append([currentDate])
}
}
currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
}
/* prints
[
[
2018-12-04 17:57:44 +0000
2018-12-05 17:57:44 +0000
2018-12-06 17:57:44 +0000
2018-12-07 17:57:44 +0000
2018-12-08 17:57:44 +0000
],
[2018-12-09 17:57:44 +0000],
[
2018-12-10 17:57:44 +0000,
2018-12-11 17:57:44 +0000,
2018-12-12 17:57:44 +0000,
2018-12-13 17:57:44 +0000
],
[2018-12-14 17:57:44 +0000],
[
2018-12-15 17:57:44 +0000,
2018-12-16 17:57:44 +0000,
2018-12-17 17:57:44 +0000,
2018-12-18 17:57:44 +0000
]
]
*/

最新更新