Reputation: 164
I'm working on an app in Swift 4.0 that uses Apple's HealthKit. I have the app working getting the user's steps from HealthKit. Here is my working code:
//sampleType declaration
let sampleType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
//define the predicate from the passed start and end times
let queryPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
//build the query
let cumulativeSumQuery = HKStatisticsQuery(quantityType: sampleType!, quantitySamplePredicate: queryPredicate, options: .cumulativeSum, completionHandler: { (cumulativeSumQuery, results, error ) in
//PROCESS THE DATA//
})
//execute the query
self.healthStore.execute(cumulativeSumQuery)
The problem is that it takes data from multiple sources. So I want to add .separateBySource as an option in my HKStatistics. Based on this question and the Apple documentation, the following code should work by simply adding | .separateBySource
to my options:
//sampleType declaration
let sampleType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
//define the predicate from the passed start and end times
let queryPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
//build the query
let cumulativeSumQuery = HKStatisticsQuery(quantityType: sampleType!, quantitySamplePredicate: queryPredicate, options: .cumulativeSum | .separateBySource, completionHandler: { (cumulativeSumQuery, results, error ) in
//PROCESS THE DATA//
})
//execute the query
self.healthStore.execute(cumulativeSumQuery)
But instead, I get the error Type of expression is ambiguous without more context
. Xcode red underlines the |
character in between my two options.
Upvotes: 1
Views: 425
Reputation: 71854
For swift 4.0 Replace:
let cumulativeSumQuery = HKStatisticsQuery(quantityType: sampleType!, quantitySamplePredicate: queryPredicate, options: .cumulativeSum | .separateBySource, completionHandler: { (cumulativeSumQuery, results, error ) in
//PROCESS THE DATA//
})
With
//build the query
let cumulativeSumQuery = HKStatisticsQuery(quantityType: sampleType!, quantitySamplePredicate: queryPredicate, options: HKStatisticsOptions(rawValue: HKStatisticsOptions.RawValue(UInt8(HKStatisticsOptions.cumulativeSum.rawValue) | UInt8(HKStatisticsOptions.separateBySource.rawValue))), completionHandler: { (cumulativeSumQuery, results, error ) in
//PROCESS THE DATA//
})
For more info check this declaration:
@enum HKStatisticsOptions
@abstract Options for specifying which statistics to calculate
@discussion When querying for HKStatistics objects, an options bitmask will specify which statistics will be
calculated.
Statistics are classified as discrete or cumulative. If a discrete statistics option is specified for a
cumulative HKQuantityType, an exception will be thrown. If a cumulative statistics options is specified
for a discrete HKQuantityType, an exception will also be thrown.
@constant HKStatisticsOptionNone
@constant HKStatisticsOptionSeparateBySource
@constant HKStatisticsOptionDiscreteAverage Calculate averageQuantity when creating statistics.
@constant HKStatisticsOptionDiscreteMin Calculate minQuantity when creating statistics.
@constant HKStatisticsOptionDiscreteMax Calculate maxQuantity when creating statistics.
@constant HKStatisticsOptionCumulativeSum Calculate sumQuantity when creating statistics.
@available(iOS 8.0, *)
public struct HKStatisticsOptions : OptionSet {
public init(rawValue: UInt)
public static var separateBySource: HKStatisticsOptions { get }
public static var discreteAverage: HKStatisticsOptions { get }
public static var discreteMin: HKStatisticsOptions { get }
public static var discreteMax: HKStatisticsOptions { get }
public static var cumulativeSum: HKStatisticsOptions { get }
}
Upvotes: 1