gpt4 book ai didi

iphone - 如何读取healthkit中的HKWorkoutActivityType.Running?

转载 作者:行者123 更新时间:2023-12-03 19:49:29 32 4
gpt4 key购买 nike

我尝试使用下面的代码从 healthapp 读取数据,但我得到的结果为 nil 值,并且还出现错误 Invalid HKObjectType HKQuantityTypeIdentifierDistanceWalkingRunning for keyPath锻炼类型。

Mac 版本:10.10.5xcode版本:7.1

let distanceType =
HKObjectType.quantityTypeForIdentifier(
HKQuantityTypeIdentifierDistanceWalkingRunning)

let workoutPredicate = HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Running)

let startDateSort =
NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)

let query = HKSampleQuery(sampleType: distanceType!, predicate: workoutPredicate,
limit: 0, sortDescriptors: [startDateSort]) {
(sampleQuery, results, error) -> Void in

if let distanceSamples = results as? [HKQuantitySample] {

// process the detailed samples...

}
else {
// Perform proper error handling here...
print("*** An error occurred while adding a sample to " +
"the workout: \(error!.localizedDescription)")

abort()
}
}

// Execute the query
healthManager.healthKitStore.executeQuery(query)

下面的代码用于访问 healthkit 数据

// 1. Set the types you want to read from HK Store
let typeOfRead = [HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBloodType)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierOxygenSaturation)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic)!]

let typeOfReads = NSSet(array: typeOfRead)

// 2. Set the types you want to write to HK Store

let typeOfWrite = [

HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!,
HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!

]

let typeOfWrites = NSSet(array: typeOfWrite)

最佳答案

使用这个:

let query = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: workoutPredicate,
limit: 0, sortDescriptors: [startDateSort]) {
// ...
}

对于sampleType,您表示您想要选择锻炼。谓词确定用于选择哪些锻炼属性。

您告诉 healthSore 选择具有锻炼属性的运行样本。这不适合在一起。

关于iphone - 如何读取healthkit中的HKWorkoutActivityType.Running?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590055/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com