ios - 在 HealthKit HKStatisticsQuery 中收到 "Type of expression is ambiguous without more context"作为错误

标签 ios swift healthkit

我正在开发一款使用 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 questionthe 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/

相关文章:

ios - 如何使用 GLKMathUnproject

ios - 添加 CollectionView 作为 TableView 标题

ios - 您如何使用 HealthKit 检测锻炼开始/停止?

iOS:如何从文本字段中获取文本并将其显示在警报中?

iOS 8 : Mapkit error throws an : Must call -[CLLocationManager requestWhenInUseAuthorization]

objective-c - 无法通过 Swift 扩展向 Objective-C NSManagedObject 子类添加方法

ios - 如何从字符串Swift中删除 '\u{ef}'字符

xcode - XCode/Swift 中缺少类

apple-watch - 当向 Apple Watch 请求健康套件许可时,requestAuthorizationToShareTypes 函数完成 block 不会被调用

ios - AVPlayer 播放准确的时间