如何在Swift中将对象组织成月份和年份?

Gar*_*abo 2 date swift

在HealthKit工作,我有一系列的HealthKit锻炼,我需要组织到月份和年份(这样我就可以显示2018年1月,2018年2月等的锻炼).让我感到困难的是我首先需要检查一个月和一年是否有锻炼,如果不是我需要为它创建阵列,如果我需要附加到现有阵列.我也不确定最好的数据模型,我正在考虑使用[[Month:Year]]但是看起来不是很Swifty?

guard let workoutsUnwrapped = workouts else { return }

for workout in workoutsUnwrapped {
    let calendar = Calendar.current
    let year = calendar.component(.year, from: workout.startDate)
    let month = calendar.component(.month, from: workout.startDate)
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 5

我首先创建struct一个年份和月份:

struct YearMonth: Comparable, Hashable {
    let year: Int
    let month: Int

    init(year: Int, month: Int) {
        self.year = year
        self.month = month
    }

    init(date: Date) {
        let comps = Calendar.current.dateComponents([.year, .month], from: date)
        self.year = comps.year!
        self.month = comps.month!
    }

    var hashValue: Int {
        return year * 12 + month
    }

    static func == (lhs: YearMonth, rhs: YearMonth) -> Bool {
        return lhs.year == rhs.year && lhs.month == rhs.month
    }

    static func < (lhs: YearMonth, rhs: YearMonth) -> Bool {
        if lhs.year != rhs.year {
            return lhs.year < rhs.year
        } else {
            return lhs.month < rhs.month
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以将其用作字典中的键,其中每个值都是一个训练数组.

var data = [YearMonth: [HKWorkout]]()
Run Code Online (Sandbox Code Playgroud)

现在迭代你的锻炼:

guard let workouts = workouts else { return }

for workout in workouts {
    let yearMonth = YearMonth(date: workout.startDate)
    var yearMonthWorkouts = data[yearMonth, default: [HKWorkout]())
    yearMonthWorkouts.append(workout)
    data[yearMonth] = yearMonthWorkouts
}
Run Code Online (Sandbox Code Playgroud)

完成此操作后,您的所有锻炼都按年/月分组.

您可以为字典中的键构建年/月的排序列表.

let sorted = data.keys.sorted()
Run Code Online (Sandbox Code Playgroud)

要将其应用于表视图,请使用sorted以定义节数.对于每个部分,从相应部分data的给定YearMonth中获取锻炼数组.

  • 从Swift 4开始,使用init(分组:by :)而不是for循环.https://developer.apple.com/documentation/swift/dictionary/2919592-init (2认同)