select HealthKit HKActivitySummary -> "NSInvalidArgumentException", "startDateComponents: Date components require a calendar."
select HealthKit HKActivitySummary -> "NSInvalidArgumentException", "startDateComponents: Date components require a calendar."
我尝试 select HealthKit activitySummaries(iOS 9.3 的新功能)
@available(iOS 9.3, watchOS 2.2, *)
func selectTodayActivitySummary (store: HKHealthStore, completion: ((summaryQuery: HKActivitySummaryQuery, summary: [HKActivitySummary]?, error: NSError?) -> Void)) {
guard let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) else {
fatalError("*** This should never fail. ***")
}
let endDate = NSDate()
guard let startDate = calendar.dateByAddingUnit(.Day, value: -7, toDate: endDate, options: []) else {
fatalError("*** unable to calculate the start date ***")
}
let units: NSCalendarUnit = [.Day, .Month, .Year, .Era]
let startDateComponents = calendar.components(units, fromDate: startDate)
let endDateComponents = calendar.components(units, fromDate:endDate)
// Create the predicate for the query
let summariesWithinRange = HKQuery.predicateForActivitySummariesBetweenStartDateComponents(startDateComponents, endDateComponents: endDateComponents)
//let summariesAtDate = HKQuery.predicateForActivitySummaryWithDateComponents(startDateComponents)
let query = HKActivitySummaryQuery(predicate: summariesWithinRange) {
(summaryQuery: HKActivitySummaryQuery, summary: [HKActivitySummary]?, error: NSError?) in
completion(summaryQuery: summaryQuery, summary: summary, error: error)
}
store.executeQuery(query)
}
当我运行将此代码作为单元测试时,出现异常
"NSInvalidArgumentException"、"startDateComponents: Date components require a calendar."
这段代码有什么问题?
这段代码几乎完全来自https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKActivitySummaryQuery_Class/index.html
使用 XCode 7.3 beta 4 (7D152p)、iPhone 6s Plus 9.3 模拟器进行测试
calendar
属性 需要在 startDateComponents
和 endDateComponents
上为非零(否则你希望组件在哪个日历中解释是不明确的) .不幸的是,当您调用 components(_, fromDate:)
时,NSCalendar
不会填充 属性 本身,因此您需要像这样手动执行此操作:
let startDateComponents = calendar.components(units, fromDate: startDate)
startDateComponents.calendar = calendar
let endDateComponents = calendar.components(units, fromDate:endDate)
endDateComponents.calendar = calendar
我尝试 select HealthKit activitySummaries(iOS 9.3 的新功能)
@available(iOS 9.3, watchOS 2.2, *)
func selectTodayActivitySummary (store: HKHealthStore, completion: ((summaryQuery: HKActivitySummaryQuery, summary: [HKActivitySummary]?, error: NSError?) -> Void)) {
guard let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) else {
fatalError("*** This should never fail. ***")
}
let endDate = NSDate()
guard let startDate = calendar.dateByAddingUnit(.Day, value: -7, toDate: endDate, options: []) else {
fatalError("*** unable to calculate the start date ***")
}
let units: NSCalendarUnit = [.Day, .Month, .Year, .Era]
let startDateComponents = calendar.components(units, fromDate: startDate)
let endDateComponents = calendar.components(units, fromDate:endDate)
// Create the predicate for the query
let summariesWithinRange = HKQuery.predicateForActivitySummariesBetweenStartDateComponents(startDateComponents, endDateComponents: endDateComponents)
//let summariesAtDate = HKQuery.predicateForActivitySummaryWithDateComponents(startDateComponents)
let query = HKActivitySummaryQuery(predicate: summariesWithinRange) {
(summaryQuery: HKActivitySummaryQuery, summary: [HKActivitySummary]?, error: NSError?) in
completion(summaryQuery: summaryQuery, summary: summary, error: error)
}
store.executeQuery(query)
}
当我运行将此代码作为单元测试时,出现异常 "NSInvalidArgumentException"、"startDateComponents: Date components require a calendar."
这段代码有什么问题? 这段代码几乎完全来自https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKActivitySummaryQuery_Class/index.html
使用 XCode 7.3 beta 4 (7D152p)、iPhone 6s Plus 9.3 模拟器进行测试
calendar
属性 需要在 startDateComponents
和 endDateComponents
上为非零(否则你希望组件在哪个日历中解释是不明确的) .不幸的是,当您调用 components(_, fromDate:)
时,NSCalendar
不会填充 属性 本身,因此您需要像这样手动执行此操作:
let startDateComponents = calendar.components(units, fromDate: startDate)
startDateComponents.calendar = calendar
let endDateComponents = calendar.components(units, fromDate:endDate)
endDateComponents.calendar = calendar