由于某些奇怪的原因,在执行HKActivitySummaryQuery
时,每个摘要的返回日期组件都落后了一天。
查询返回正确日期的数据,但数据的dateComponents
日期落后一天。我尝试过设置时区和区域设置,但结果保持不变。
struct ActivitySummary {
init?(_ summary: HKActivitySummary) {
var calendar = Calendar.current
calendar.timeZone = TimeZone.current
guard let date = summary.dateComponents(for: calendar).date else { return nil }
print("ORIGINAL: ", date.description(with: Locale.current))
//Expected: Tuesday, January 30, 2018 at 7:00:00 PM Eastern Standard Time
//Results: Monday, January 29, 2018 at 7:00:00 PM Eastern Standard Time
let other = calendar.dateComponents( [ .year, .month, .day ], from: date)
print("START OF DAY: ", date.startOfDay.description(with: Locale.current))
//Expected: Tuesday, January 30, 2018 at 12:00:00 AM Eastern Standard Time
//Results: Monday, January 29, 2018 at 12:00:00 AM Eastern Standard Time
}
}
func summaryQuery(){
let predicate = HKQuery.predicate(forActivitySummariesBetweenStart: fromDate.components(), end: toDate!.components())
let query = HKActivitySummaryQuery(predicate: predicate) { (query, summaries, error) in
guard let summaries = summaries, summaries.count > 0 else {
return
}
//
var activitySummaries: [ActivitySummary] = []
activitySummaries = summaries.compactMap({
ActivitySummary($0)
})
}
}
答案 0 :(得分:1)
也许您正在使用的日历是错误的。像这样设置你的日历:
let calendar = Calendar.current