【发布时间】:2019-05-06 03:10:09
【问题描述】:
简介
我正在创建一个日历应用程序,我需要管理一个日期数组以便为 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
}
感谢您阅读我的问题。
【问题讨论】:
-
不是'你的两个分支完全一样吗?为什么不使用空数组而不是可选的
dateSections?然后你可以使用dateSections.append,甚至map。 -
@Sulthan 感谢您的评论!这是一个有效的观点,因为
dateSection永远不会为零。是的,我只是注意到我把它复制错了。尴尬,我会用它来编辑我的问题,但是我的实际尝试并没有更好。然而,问题是我无法弄清楚如何正确地对算法进行语法分析,我显然还不足以成为一个使用 swift 语法的朋友。您介意在答案中展示一个快速示例,说明我如何制定 map 方法吗?下面的当前答案尝试留下了一些问题。 -
请添加正确的代码。如果您的代码正常工作,codereview.stackexchange.com 可能是一个更好的地方。
-
@Sulthan 看来我在手机上写了上面的评论有点仓促......我已经用我当前的代码更新了我的问题。是的,它暂时有效,但是 Nivs 的回答让我相信这是执行该算法的一种不好的方法,因此我想学习如何有效地/在良好的实践中做到这一点。如果我可以咨询您:这是保持问题公开的正当理由吗?还是我应该使用代码审查姐妹网站?
标签: ios arrays swift sorting date