无法从锻炼元数据获取游泳位置

时间:2018-10-04 20:27:53

标签: swift metadata health-kit

我试图列出游泳锻炼的所有数据,并在控制台上显示HKMetadataKeySwimmingLocationType的值。

覆盖func viewDidLoad(){         super.viewDidLoad()

    // print on console all the "root" objects

    print ("//////////-------- NEW WORKOUT --------/////////")

    if let startWorkout = workout?.startDate {
        print("Start Date: \(startWorkout.description(with: Locale.current))")
    }

    if let endWorkout = workout?.endDate {
        print("End Date: \(endWorkout.description(with: Locale.current))")
    }

    if let totalEnergyBurned = workout?.totalEnergyBurned {
        print("Total Energy Burned: \(totalEnergyBurned.doubleValue(for: HKUnit.kilocalorie())) kcal")
    }

    if let totalDistance = workout?.totalDistance{
        print("Total Distance: \(totalDistance.doubleValue(for: HKUnit.meter())) m")
    }

    if let totalDuration = workout?.duration {
        print("Total duration: \(formatTI(duration: totalDuration))") 
    }

    if let totalStrokeCount = workout?.totalSwimmingStrokeCount
    {
        print("Total Stroke Count: \(totalStrokeCount.doubleValue(for: HKUnit.count())) strokes")
    }

    if let metadatasWorkout =  workout?.metadata{
        for (key,value) in metadatasWorkout {
            switch key {
            case HKMetadataKeyLapLength:
                print ("\(key.description) : \(value)")
            case HKMetadataKeyIndoorWorkout:
                print ("\(key.description) : \(value)")
            case HKMetadataKeySwimmingLocationType:
                switch value {
                    case HKWorkoutSwimmingLocationType.pool:
                        print ("\(key.description) : POOL")
                    case HKWorkoutSwimmingLocationType.openWater:
                        print ("\(key.description) : OPENWATER")
                    case HKWorkoutSwimmingLocationType.unknown:
                        print ("\(key.description) : UNKNOWN")
                    default:
                        print ("\(key.description) : OTHER")
                        print ("\(key.description) : \(value)")
                }

            case HKMetadataKeySwimmingStrokeStyle:
                print ("\(key.description) : \(value)")
            default:
                print ("Default!! \(key.description) : \(value)")
            }
        }

}

具有以下输出:

HealthKit成功获得授权。 ////////// --------新锻炼-------- ////////// 开始日期:2018年10月1日星期一中欧夏令时 结束日期:2018年10月1日,星期一19:26:03中欧夏季时间 消耗总能量:112.20791988510304 kcal 总距离:550.0 m 总时长:13m 总笔画数:250.0次 HKSwimmingLocationType:其他 HKSwimmingLocationType:1

我的问题是:

由于HKWorkoutSwimmingLocationType的值为1,为什么系统未执行以下行?

案例HKWorkoutSwimmingLocationType.pool:      打印(“((key.description):POOL”)

在Apple文档上,HKWorkoutSwimmingLocationType.pool的值为1 ...

0 个答案:

没有答案