swift - HKQuantity to Double - getting Active Energy Value from Healthkit -


i'm working on method reads active energy (kcal) health kit have problem getting double value hkquantity. code looks this:

func getactiveenergy () {     let enddate = nsdate()     let startdate = nscalendar.currentcalendar().datebyaddingunit(.month, value: -1, todate: enddate, options: [])      let energysampletype = hksampletype.quantitytypeforidentifier(hkquantitytypeidentifieractiveenergyburned)     let predicate = hkquery.predicateforsampleswithstartdate(startdate, enddate: enddate, options: .none)      print ("start date: ", startdate)     print ("end date: ", enddate)      let query = hksamplequery(sampletype: energysampletype!, predicate: predicate, limit: 0, sortdescriptors: nil, resultshandler: {         (query, results, error) in         if results == nil {             print("there error running query: \(error)")         }          dispatch_async(dispatch_get_main_queue()) {              activity in results as! [hkquantitysample]             {                 self.todayactiveenergy = activity.quantity.doublevalueforunit(hkunit.countunit())                 print(">>>>>", self.todayactiveenergy)             }          }     })     self.healthkitstore.executequery(query) } 

my problem row:

 self.todayactiveenergy = activity.quantity.doublevalueforunit(hkunit.countunit()) 

the activity.quantity returns indeed right value (156 kcal) when try getting double value (as done above) libc++abi.dylib: terminating uncaught exception of type nsexception

any idea why might happen?

thanks ooper comment. changing line to:

self.todayactiveenergy = activity.quantity.doublevalueforunit(hkunit.kilocalorieunit()) 

did trick.


Comments