在HealthKit HKStatisticsQuery中接收“没有更多上下文的表达类型是不明确的”作为错误

问题描述 投票:1回答:1

我正在研究使用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 red强调了我的两个选项之间的|字符。

ios swift health-kit
1个回答
1
投票

对于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 }
}
© www.soinside.com 2019 - 2024. All rights reserved.