2015-10-15 57 views

回答

3

如果你有一個HKQuantitySample對象,那麼你可以在quantity財產使用doubleValueForUnit功能就像下面的方式轉換成Int

let value = Int(sample.quantity.doubleValueForUnit(HKUnit.countUnit())) 

sampleHKQuantitySample

記住,你需要當你通過可選的結合像這樣適當地投樣本:

readFullSample(anyValueYouPassHere) { samples in 
    for sample in samples as [HKQuantitySample] { 
     let value = Int(sample.quantity.doubleValueForUnit(HKUnit.countUnit())) 
    } 
} 

我希望這可以幫助您。

+0

我得到這個:二零一五年十月十五日14:05:53.135出口衛生[79376:4267474 ***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:'嘗試轉換不兼容的單元:m,count'..... libC++ abi.dylib:終止於類型爲NSException的未捕獲異常 – Zander

+0

請將您的完整查詢從何處獲取'HKQuantitySample'對象 –

+0

FUNC readFullSample(sampleType:HKSampleType,完成:(([HKQuantitySample]) - >空隙)){ 讓stepsSampleQuery = HKSampleQuery(sampleType:sampleType, 謂詞:無, 極限:100, sortDescriptors:nil) {(query,results,error)in if let results = results as? [HKQuantitySample] { 完成(結果) } } //不要忘記執行查詢! self.healthKitStore.executeQuery(stepsSampleQuery) } – Zander

相關問題