0
私はインターネットの周りを見ていて、developer.apple.comのドキュメントに関連性があると読んでいます。クエリを設定する方法を扱ういくつかのチュートリアルがありますが、どのクエリでも返されたHKSamplesからデータ値を抽出する簡単な方法が見つかりませんでした。 この場合、文字列処理よりも優雅で汎用性のあるものがなければなりません。ヘルスケアから賢明にデータを取得する方法HKSample迅速なクエリで返された場合
私のためによく働く私が持っている相続コード:
var anchor : HKQueryAnchor? = nil
func newStreamingQuery(withIdentifier: HKQuantityTypeIdentifier) {
let hkqType = HKQuantityType.quantityType(forIdentifier: withIdentifier)!
let observer = HKObserverQuery(sampleType: hkqType, predicate: nil, updateHandler:
{(queryResult, completionHandler, error) in
if (error != nil) { print(error!.localizedDescription) ; return}
// We recreate the query every time becuase it can only be used once.
let query = HKAnchoredObjectQuery(type: hkqType, predicate: nil, anchor: self.anchor, limit: HKObjectQueryNoLimit, resultsHandler: { (query, samples, deleted, newAnchor, error) in
if error != nil {print(error!) ; return}
self.anchor = newAnchor
print ("\(samples!.count)\n")
for i in (0..<samples!.count)
{ // What do i do to access the data in s sensible fassion.
let hktype = samples![i]
let value = hktype// do something with hktype to get the value out
let time = hktype// do something with hktype to get the time out
print("\(value)\n")}
completionHandler()
})
WatchHelthKitHelper.healthStore.execute(query)
})
WatchHelthKitHelper.healthStore.execute(observer)
}
私はこのようにそれを呼び出す場合:
1
1.56667 count/s 723404AC-C90C-4374-B75A-AAE7CD166115 "Jason’s Apple Watch" (3.1) "Apple Watch" (2017-01-02 15:59:42 +0800 - 2017-01-02 15:59:42 +0800)
1
1.63333 count/s 148B311D-57EE-4D65-B560-765E7E8319C6 "Jason’s Apple Watch" (3.1) "Apple Watch" (2017-01-02 15:59:46 +0800 - 2017-01-02 15:59:46 +0800)
1
1.65 count/s F92DD3AE-0527-48D1-B75B-3F04D6C0ABCF "Jason’s Apple Watch" (3.1) "Apple Watch" (2017-01-02 15:59:49 +0800 - 2017-01-02 15:59:49 +0800)
1
1.63333 count/s 0CF55099-6D51-408D-B322-00A8F469AF54 "Jason’s Apple Watch" (3.1) "Apple Watch" (2017-01-02 15:59:53 +0800 - 2017-01-02 15:59:53 +0800)
1
1.48333 count/s 1FCD3472-081F-4479-80F2-543A45CCA221 "Jason’s Apple Watch" (3.1) "Apple Watch" (2017-01-02 16:00:00 +0800 - 2017-01-02 16:00:00 +0800)
:
func startStreamingQuery(){
newStreamingQuery(withIdentifier: HKQuantityTypeIdentifier.heartRate)
}
私はこのような素敵な出力を得ます私はこれを行うのは簡単だと確信していますが、私は現時点で理解していない概念の下に隠されなければならないと感じています。
あなたの回答には非常に感謝しています。
また、詳細な心臓波/脈拍データIDを取得する方法を知っている人は、それについて聞くのが大好きです。
[これ](http://stackoverflow.com/questions/39653438/getting-yesterdays-steps-from-healthkit/39654164#39654164)を助けることができます。 –
ありがとうございましたUmairとSivajeeあなたはどちらも正しい軌道に乗っていますが、以下の答えは書かれた方法ではコンパイルされませんでした。 'heartRateUnit = HKUnit(from: "count/min")に少し変更が必要です。 print "(心拍数:\(currData.quantity.doubleValue(for:heartRateUnit))") ' –