ios - 按自定义时间间隔对对象数组进行分组
问题描述
我有一系列帖子,我目前按天分组。我使用以下功能来做到这一点;
private func splitDay(from date: Date) -> Date {
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
return calendar.date(from: components)!
}
private func sectionPosts(posts: [Post]) {
let groups = Dictionary(grouping: posts) { (posts) in
return splitDay(from: Date(timeIntervalSince1970: posts.createdOn))
}
self.sections = groups.map { (date, posts) in
return PostSection(date: date, posts: posts)
}
}
但是,我想实现一个自定义分组,如下所示;
- 今天
- 昨天
- 本星期
- 这个月
- 年长者
我将如何将其构建到我的分组功能中?我的部分结构是这样的;
struct PostSection {
var date: Date
var posts: [Post]
}
解决方案
由于您尝试将数据分组为“今天”、“昨天”、“本周”和“本月”等组,因此您应该首先创建一个类型来表示这些组:
enum PostGroup {
case today
case yesterday
case thisWeek
case thisMonth
case older
case coming // added "coming" group so that the groups cover all possible dates
}
然后你的PostSection
结构将有一个PostGroup
属性,而不是一个Date
属性:
struct PostSection {
let group: PostGroup
let posts: [Post]
}
现在我们只需要一个(Post) -> PostGroup
可以传递给Dictionary(grouping:by:)
. 这可以通过将发布日期的日期组件与今天的日期组件进行比较来实现:
func group(for post: Post) -> PostGroup {
let today = Date()
let calendar = Calendar.current
let postDateComponents = calendar.dateComponents([.year, .month, .day], from: post.createdOn)
let todayDateComponents = calendar.dateComponents([.year, .month, .day], from: today)
if postDateComponents == todayDateComponents {
return .today
}
let daysDifference = calendar.dateComponents([.day], from: postDateComponents, to: todayDateComponents)
if daysDifference.day == 1 {
return .yesterday
}
let postWeekComponents = calendar.dateComponents([.weekOfYear, .yearForWeekOfYear], from: post.createdOn)
let todayWeekComponents = calendar.dateComponents([.weekOfYear, .yearForWeekOfYear], from: today)
if postWeekComponents == todayWeekComponents {
return .thisWeek
}
if postDateComponents.year == todayDateComponents.year &&
postDateComponents.month == todayDateComponents.month {
return .thisMonth
}
if post.createdOn < today {
return .older
} else {
return .coming
}
}
要完成它:
private func sectionPosts(posts: [Post]) {
let groups = Dictionary(grouping: posts, by: group(for:))
self.sections = groups.map { (group, posts) in
return PostSection(group: group, posts: posts)
}
}
推荐阅读
- python - 如何增量更新数组
- javascript - .then() 方法返回的承诺中承诺了什么?
- java - 为什么我的服务器会跳过 if 语句并转到 else 部分(再试一次)?
- python - 按播放时间停止视频
- amazon-web-services - 我可以向 AWS SNS 发送批处理消息吗
- mysql - 使用 IN 子句从两个表中获取数据
- jquery - 如何在使用 ArcGIS API JS 加载 KMZ 文件的 DOM Construct 之前将 http 替换为 https
- javascript - 单击两个输入的相同标签名称的关闭图标时,取消选中选中的输入
- linux - 如何获得 HDFS Quota 的逗号/制表符分隔输出?
- json - 使用 OPENJSON 解析 JSON 时语法不正确