'attempt to convert incompatible units' error on HealthKit Statistics query (swift)
<p>I am needing to calculate the cumulative sum of calories, and am getting the error 'NSInvalidArgumentException', reason: 'Attempt to convert incompatible units: kcal, count'</p>

<p>on the line:</p>

<pre><code>let numberOfCalories = Int(sumQuantity.doubleValueForUnit(self.caloriesUnit))
</code></pre>

<p>My unit has been defined as:</p>

<pre><code>let caloriesUnit = HKUnit.countUnit()
</code></pre>

<p>Should it be something different? How can I return the number of calories?</p>

<p>my function is:</p>

<pre><code>func queryCaloriesSum() {

    let caloriesQuantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned) //else //{ return nil }

    let sumOption = HKStatisticsOptions.CumulativeSum
    let statisticsSumQuery = HKStatisticsQuery(quantityType: caloriesQuantityType!, quantitySamplePredicate: nil, options: sumOption) { [unowned self] (query, result, error) in
        if let sumQuantity = result?.sumQuantity() {

           // let headerView = self.tableView.dequeueReusableCellWithIdentifier(self.totalStepsCellIdentifier)! as UITableViewCell

            let numberOfCalories = Int(sumQuantity.doubleValueForUnit(self.caloriesUnit))

        print(numberOfCalories)
        }

    }
    healthStore.executeQuery(statisticsSumQuery)
    print(statisticsSumQuery)

}
</code></pre>