2017-12-23 28 views
1

AppleのHealthKitを使用するSwift 4.0のアプリケーションで作業しています。私は、アプリケーションがHealthKitからユーザーの手順を取得している。ここに私の作業コードがあります:HealthKit HKStatisticsQueryのエラーとして「式の種類が曖昧です」

//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に基づいて、次のコードは、単に私の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) 

| .separateBySourceを追加することで作業が必要がありますが、その代わり、私はエラーType of expression is ambiguous without more contextを取得します。 Xcode赤は、私の2つのオプションの間に|文字の下線を引いています。 SWIFT 4.0の場合

答えて

1

は交換してください:

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 } 
} 
関連する問題