I have made this function to get all steps for each day but I want to convert this function from swift 2.x to swift 3. But I get stuck on 2 different points.
let anchorDate = calendar.dateComponents(anchorComponents)
I get this error: Cannot invoke dateCompontents with an argument of list type (Datecomponents)
And on this point:
let stepsQuery = HKStatisticsCollectionQuery(quantityType: HKQuantityType, quantitySamplePredicate: nil, options: .CumulativeSum, anchorDate: anchorComponents!, intervalComponents: interval)
Error: Cannot convert value of type HKQuantityType.Type to expected argument type HKQuantityType.
The complete function:
func getDailySteps(){
let calendar = NSCalendar.current
let interval = NSDateComponents()
interval.day = 1
var anchorComponents = calendar.dateComponents([.day , .month , .year], from: NSDate() as Date)
anchorComponents.hour = 0
let anchorDate = calendar.dateComponents(anchorComponents)
// Define 1-day intervals starting from 0:00
let stepsQuery = HKStatisticsCollectionQuery(quantityType: HKQuantityType, quantitySamplePredicate: nil, options: .CumulativeSum, anchorDate: anchorComponents!, intervalComponents: interval)
// Set the results handler
stepsQuery.initialResultsHandler = {query, results, error in
let endDate = NSDate()
let startDate = calendar.dateByAddingUnit(.Day, value: -7, toDate: endDate, options: [])
if let myResults = results{ myResults.enumerateStatisticsFromDate(startDate!, toDate: endDate) { statistics, stop in
if let quantity = statistics.sumQuantity(){
let date = statistics.startDate
let steps = quantity.doubleValueForUnit(HKUnit.countUnit())
print("\(date): steps = \(steps)")
//NOTE: If you are going to update the UI do it in the main thread
dispatch_async(dispatch_get_main_queue()){
//update UI components
}
}
} //end block
} //end if let
}
HKHealthStore?.executeQuery(stepsQuery)
}
So how can I convert this method to swift 3 ?