我正在开发一款使用 Apple 的 HealthKit 的 Swift 4.0 应用程序。我的应用程序可以从 HealthKit 获取用户的步数。这是我的工作代码:
//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)
问题是它从多个来源获取数据。所以我想在我的 HKStatistics 中添加 .separateBySource 作为一个选项。基于this question和 the Apple documentation ,下面的代码应该通过简单地添加 | 来工作.separateBySource
到我的 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)
但相反,我收到错误 Type of expression is ambiguous without more context
。 Xcode 红色强调了我两个选项之间的 |
字符。
最佳答案
对于 swift 4.0 替换:
let cumulativeSumQuery = HKStatisticsQuery(quantityType: sampleType!, quantitySamplePredicate: queryPredicate, options: .cumulativeSum | .separateBySource, completionHandler: { (cumulativeSumQuery, results, error ) in
//PROCESS THE DATA//
})
与
//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//
})
有关更多信息,请查看此声明:
@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 }
}
关于ios - 在 HealthKit HKStatisticsQuery 中收到 "Type of expression is ambiguous without more context"作为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949815/