在 HealthKit HKStatisticsQuery 中接收 "Type of expression is ambiguous without more context" 作为错误
Receiving "Type of expression is ambiguous without more context" as an error in a HealthKit HKStatisticsQuery
我正在开发 Swift 4.0 中使用 Apple 的 HealthKit 的应用程序。我的应用程序可以从 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 and 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 }
}
我正在开发 Swift 4.0 中使用 Apple 的 HealthKit 的应用程序。我的应用程序可以从 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 and 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 }
}