如何在 HealthKit 中获取 HKWorkoutActivityType 的名称?

Posted

技术标签:

【中文标题】如何在 HealthKit 中获取 HKWorkoutActivityType 的名称?【英文标题】:How to get the name of HKWorkoutActivityType in HealthKit? 【发布时间】:2015-05-11 18:42:19 【问题描述】:

好的,所以HKWorkout 的属性workoutActivityType 返回一个NSInteger

这里是活动列表:workoutActivityType list

现在,我将创建一个plist,并在获得integer 时查找活动名称。但必须经历这些似乎有点奇怪。

我是否需要自己找出是哪项活动,还是我缺少什么?

【问题讨论】:

【参考方案1】:

对于现在仍然遇到此问题的任何人,我已经在 swift 中组合了一个简单的 HKWorkoutActivityType 扩展,以允许将这些类型映射到用户友好的字符串,并在可用的情况下映射到适当的表情符号表示:https://github.com/georgegreenoflondon/HKWorkoutActivityType-Descriptions/。

这个问题的相关sn-p是:

extension HKWorkoutActivityType 

    /*
     Simple mapping of available workout types to a human readable name.
     */
    var name: String 
        switch self 
        case .americanFootball:             return "American Football"
        case .archery:                      return "Archery"
        case .australianFootball:           return "Australian Football"
        case .badminton:                    return "Badminton"
        case .baseball:                     return "Baseball"
        case .basketball:                   return "Basketball"
        case .bowling:                      return "Bowling"
        case .boxing:                       return "Boxing"
        case .climbing:                     return "Climbing"
        case .crossTraining:                return "Cross Training"
        case .curling:                      return "Curling"
        case .cycling:                      return "Cycling"
        case .dance:                        return "Dance"
        case .danceInspiredTraining:        return "Dance Inspired Training"
        case .elliptical:                   return "Elliptical"
        case .equestrianSports:             return "Equestrian Sports"
        case .fencing:                      return "Fencing"
        case .fishing:                      return "Fishing"
        case .functionalStrengthTraining:   return "Functional Strength Training"
        case .golf:                         return "Golf"
        case .gymnastics:                   return "Gymnastics"
        case .handball:                     return "Handball"
        case .hiking:                       return "Hiking"
        case .hockey:                       return "Hockey"
        case .hunting:                      return "Hunting"
        case .lacrosse:                     return "Lacrosse"
        case .martialArts:                  return "Martial Arts"
        case .mindAndBody:                  return "Mind and Body"
        case .mixedMetabolicCardioTraining: return "Mixed Metabolic Cardio Training"
        case .paddleSports:                 return "Paddle Sports"
        case .play:                         return "Play"
        case .preparationAndRecovery:       return "Preparation and Recovery"
        case .racquetball:                  return "Racquetball"
        case .rowing:                       return "Rowing"
        case .rugby:                        return "Rugby"
        case .running:                      return "Running"
        case .sailing:                      return "Sailing"
        case .skatingSports:                return "Skating Sports"
        case .snowSports:                   return "Snow Sports"
        case .soccer:                       return "Soccer"
        case .softball:                     return "Softball"
        case .squash:                       return "Squash"
        case .stairClimbing:                return "Stair Climbing"
        case .surfingSports:                return "Surfing Sports"
        case .swimming:                     return "Swimming"
        case .tableTennis:                  return "Table Tennis"
        case .tennis:                       return "Tennis"
        case .trackAndField:                return "Track and Field"
        case .traditionalStrengthTraining:  return "Traditional Strength Training"
        case .volleyball:                   return "Volleyball"
        case .walking:                      return "Walking"
        case .waterFitness:                 return "Water Fitness"
        case .waterPolo:                    return "Water Polo"
        case .waterSports:                  return "Water Sports"
        case .wrestling:                    return "Wrestling"
        case .yoga:                         return "Yoga"

        // ios 10
        case .barre:                        return "Barre"
        case .coreTraining:                 return "Core Training"
        case .crossCountrySkiing:           return "Cross Country Skiing"
        case .downhillSkiing:               return "Downhill Skiing"
        case .flexibility:                  return "Flexibility"
        case .highIntensityIntervalTraining:    return "High Intensity Interval Training"
        case .jumpRope:                     return "Jump Rope"
        case .kickboxing:                   return "Kickboxing"
        case .pilates:                      return "Pilates"
        case .snowboarding:                 return "Snowboarding"
        case .stairs:                       return "Stairs"
        case .stepTraining:                 return "Step Training"
        case .wheelchairWalkPace:           return "Wheelchair Walk Pace"
        case .wheelchairRunPace:            return "Wheelchair Run Pace"

        // iOS 11
        case .taiChi:                       return "Tai Chi"
        case .mixedCardio:                  return "Mixed Cardio"
        case .handCycling:                  return "Hand Cycling"

        // iOS 13
        case .discSports:                   return "Disc Sports"
        case .fitnessGaming:                return "Fitness Gaming"

        // Catch-all
        default:                            return "Other"
        
    


【讨论】:

哇,你是个传奇。谢谢你这样做!节省了我很多时间:) 你确实是个传奇【参考方案2】:

不幸的是,苹果只提供了一个枚举,你必须实现你自己的名字。一个好主意是懒惰地实例化一个字典,包括NSNumber(对象包装NSInteger)的键和字符串的值。应该很容易制作,不幸的是唯一的解决方案。

【讨论】:

仅供参考,有人在这里制作了一个常量包装器github.com/openmhealth/Granola/blob/master/Pod/Classes/… 感谢分享@shawnwall【参考方案3】:

您可以编写自己的解析器,从该页面获取string https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype

我们应该编写生成的代码,并在 iOS 应用程序中运行它。这样HKWorkoutActivityTyperawValue 总是正确且最新的

private static func makeCode() 
  let pattern = "case \\w*"
  let range = NSMakeRange(0, string.count-1)
  let regex = try! NSRegularExpression(pattern: pattern, options: [])
  regex
    .matches(in: string, options: [], range: range)
    .forEach( result in
      let start = string.index(string.startIndex, offsetBy: result.range.lowerBound)
      let end = string.index(string.startIndex, offsetBy: result.range.upperBound)
      let substring = String(string[start...end])
      let name = substring
        .replacingOccurrences(of: "case", with: "")
        .replacingOccurrences(of: " ", with: "")
        .replacingOccurrences(of: "\n", with: "")

      print("dictionary[HKWorkoutActivityType.\(name).rawValue] = \"\(name)\"")
    )

在iOS app中运行生成的代码,可以得到

(lldb) po dictionary
▿ 75 elements
  ▿ 0 : 2 elements
    - key : 36
    - value : "rugby"
  ▿ 1 : 2 elements
    - key : 71
    - value : "wheelchairRunPace"
  ▿ 2 : 2 elements
    - key : 46
    - value : "swimming"
  ▿ 3 : 2 elements
    - key : 20
    - value : "functionalStrengthTraining"
  ▿ 4 : 2 elements
    - key : 55
    - value : "waterSports"
  ▿ 5 : 2 elements
    - key : 40
    - value : "snowSports"
  ▿ 6 : 2 elements
    - key : 63
    - value : "highIntensityIntervalTraining"
  ▿ 7 : 2 elements
    - key : 69
    - value : "stepTraining"
  ▿ 8 : 2 elements
    - key : 13
    - value : "cycling"
  ▿ 9 : 2 elements
    - key : 22
    - value : "gymnastics"
  ▿ 10 : 2 elements
    - key : 43
    - value : "squash"
  ▿ 11 : 2 elements
    - key : 62
    - value : "flexibility"
  ▿ 12 : 2 elements
    - key : 35
    - value : "rowing"
  ▿ 13 : 2 elements
    - key : 65
    - value : "kickboxing"
  ▿ 14 : 2 elements
    - key : 16
    - value : "elliptical"
  ▿ 15 : 2 elements
    - key : 34
    - value : "racquetball"
  ▿ 16 : 2 elements
    - key : 32
    - value : "play"
  ▿ 17 : 2 elements
    - key : 3000
    - value : "other"
  ▿ 18 : 2 elements
    - key : 18
    - value : "fencing"
  ▿ 19 : 2 elements
    - key : 60
    - value : "crossCountrySkiing"
  ▿ 20 : 2 elements
    - key : 52
    - value : "walking"
  ▿ 21 : 2 elements
    - key : 11
    - value : "crossTraining"
  ▿ 22 : 2 elements
    - key : 66
    - value : "pilates"
  ▿ 23 : 2 elements
    - key : 58
    - value : "barre"
  ▿ 24 : 2 elements
    - key : 45
    - value : "surfingSports"
  ▿ 25 : 2 elements
    - key : 50
    - value : "traditionalStrengthTraining"
  ▿ 26 : 2 elements
    - key : 23
    - value : "handball"
  ▿ 27 : 2 elements
    - key : 3
    - value : "australianFootball"
  ▿ 28 : 2 elements
    - key : 49
    - value : "trackAndField"
  ▿ 29 : 2 elements
    - key : 30
    - value : "mixedMetabolicCardioTraining"
  ▿ 30 : 2 elements
    - key : 27
    - value : "lacrosse"
  ▿ 31 : 2 elements
    - key : 28
    - value : "martialArts"
  ▿ 32 : 2 elements
    - key : 29
    - value : "mindAndBody"
  ▿ 33 : 2 elements
    - key : 15
    - value : "danceInspiredTraining"
  ▿ 34 : 2 elements
    - key : 4
    - value : "badminton"
  ▿ 35 : 2 elements
    - key : 54
    - value : "waterPolo"
  ▿ 36 : 2 elements
    - key : 25
    - value : "hockey"
  ▿ 37 : 2 elements
    - key : 7
    - value : "bowling"
  ▿ 38 : 2 elements
    - key : 68
    - value : "stairs"
  ▿ 39 : 2 elements
    - key : 70
    - value : "wheelchairWalkPace"
  ▿ 40 : 2 elements
    - key : 59
    - value : "coreTraining"
  ▿ 41 : 2 elements
    - key : 33
    - value : "preparationAndRecovery"
  ▿ 42 : 2 elements
    - key : 21
    - value : "golf"
  ▿ 43 : 2 elements
    - key : 51
    - value : "volleyball"
  ▿ 44 : 2 elements
    - key : 38
    - value : "sailing"
  ▿ 45 : 2 elements
    - key : 24
    - value : "hiking"
  ▿ 46 : 2 elements
    - key : 1
    - value : "americanFootball"
  ▿ 47 : 2 elements
    - key : 44
    - value : "stairClimbing"
  ▿ 48 : 2 elements
    - key : 41
    - value : "soccer"
  ▿ 49 : 2 elements
    - key : 37
    - value : "running"
  ▿ 50 : 2 elements
    - key : 17
    - value : "equestrianSports"
  ▿ 51 : 2 elements
    - key : 42
    - value : "softball"
  ▿ 52 : 2 elements
    - key : 47
    - value : "tableTennis"
  ▿ 53 : 2 elements
    - key : 2
    - value : "archery"
  ▿ 54 : 2 elements
    - key : 6
    - value : "basketball"
  ▿ 55 : 2 elements
    - key : 57
    - value : "yoga"
  ▿ 56 : 2 elements
    - key : 72
    - value : "taiChi"
  ▿ 57 : 2 elements
    - key : 73
    - value : "mixedCardio"
  ▿ 58 : 2 elements
    - key : 67
    - value : "snowboarding"
  ▿ 59 : 2 elements
    - key : 56
    - value : "wrestling"
  ▿ 60 : 2 elements
    - key : 53
    - value : "waterFitness"
  ▿ 61 : 2 elements
    - key : 5
    - value : "baseball"
  ▿ 62 : 2 elements
    - key : 8
    - value : "boxing"
  ▿ 63 : 2 elements
    - key : 9
    - value : "climbing"
  ▿ 64 : 2 elements
    - key : 39
    - value : "skatingSports"
  ▿ 65 : 2 elements
    - key : 26
    - value : "hunting"
  ▿ 66 : 2 elements
    - key : 19
    - value : "fishing"
  ▿ 67 : 2 elements
    - key : 48
    - value : "tennis"
  ▿ 68 : 2 elements
    - key : 61
    - value : "downhillSkiing"
  ▿ 69 : 2 elements
    - key : 64
    - value : "jumpRope"
  ▿ 70 : 2 elements
    - key : 14
    - value : "dance"
  ▿ 71 : 2 elements
    - key : 74
    - value : "handCycling"
  ▿ 72 : 2 elements
    - key : 12
    - value : "curling"
  ▿ 73 : 2 elements
    - key : 10
    - value : "cricket"
  ▿ 74 : 2 elements
    - key : 31
    - value : "paddleSports"

【讨论】:

【参考方案4】:
extension HKWorkoutActivityType 
    var name: String 
        switch self 
        case .americanFootball:             return "American Football"
        case .archery:                      return "Archery"
        case .australianFootball:           return "Australian Football"
        case .badminton:                    return "Badminton"
        case .baseball:                     return "Baseball"
        case .basketball:                   return "Basketball"
        case .bowling:                      return "Bowling"
        case .boxing:                       return "Boxing"
        case .climbing:                     return "Climbing"
        case .cricket:                      return "Cricket"
        case .crossTraining:                return "Cross Training"
        case .curling:                      return "Curling"
        case .cycling:                      return "Cycling"
        case .dance:                        return "Dance"
        case .danceInspiredTraining:        return "Dance Inspired Training"
        case .elliptical:                   return "Elliptical"
        case .equestrianSports:             return "Equestrian Sports"
        case .fencing:                      return "Fencing"
        case .fishing:                      return "Fishing"
        case .functionalStrengthTraining:   return "Functional Strength Training"
        case .golf:                         return "Golf"
        case .gymnastics:                   return "Gymnastics"
        case .handball:                     return "Handball"
        case .hiking:                       return "Hiking"
        case .hockey:                       return "Hockey"
        case .hunting:                      return "Hunting"
        case .lacrosse:                     return "Lacrosse"
        case .martialArts:                  return "Martial Arts"
        case .mindAndBody:                  return "Mind and Body"
        case .mixedMetabolicCardioTraining: return "Mixed Metabolic Cardio Training"
        case .paddleSports:                 return "Paddle Sports"
        case .play:                         return "Play"
        case .preparationAndRecovery:       return "Preparation and Recovery"
        case .racquetball:                  return "Racquetball"
        case .rowing:                       return "Rowing"
        case .rugby:                        return "Rugby"
        case .running:                      return "Running"
        case .sailing:                      return "Sailing"
        case .skatingSports:                return "Skating Sports"
        case .snowSports:                   return "Snow Sports"
        case .soccer:                       return "Soccer"
        case .softball:                     return "Softball"
        case .squash:                       return "Squash"
        case .stairClimbing:                return "Stair Climbing"
        case .surfingSports:                return "Surfing Sports"
        case .swimming:                     return "Swimming"
        case .tableTennis:                  return "Table Tennis"
        case .tennis:                       return "Tennis"
        case .trackAndField:                return "Track and Field"
        case .traditionalStrengthTraining:  return "Traditional Strength Training"
        case .volleyball:                   return "Volleyball"
        case .walking:                      return "Walking"
        case .waterFitness:                 return "Water Fitness"
        case .waterPolo:                    return "Water Polo"
        case .waterSports:                  return "Water Sports"
        case .wrestling:                    return "Wrestling"
        case .yoga:                         return "Yoga"

        // - iOS 10

        case .barre:                        return "Barre"
        case .coreTraining:                 return "Core Training"
        case .crossCountrySkiing:           return "Cross Country Skiing"
        case .downhillSkiing:               return "Downhill Skiing"
        case .flexibility:                  return "Flexibility"
        case .highIntensityIntervalTraining:    return "High Intensity Interval Training"
        case .jumpRope:                     return "Jump Rope"
        case .kickboxing:                   return "Kickboxing"
        case .pilates:                      return "Pilates"
        case .snowboarding:                 return "Snowboarding"
        case .stairs:                       return "Stairs"
        case .stepTraining:                 return "Step Training"
        case .wheelchairWalkPace:           return "Wheelchair Walk Pace"
        case .wheelchairRunPace:            return "Wheelchair Run Pace"

        // - iOS 11

        case .taiChi:                       return "Tai Chi"
        case .mixedCardio:                  return "Mixed Cardio"
        case .handCycling:                  return "Hand Cycling"

        // - iOS 13

        case .discSports:                   return "Disc Sports"
        case .fitnessGaming:                return "Fitness Gaming"

        // - iOS 14
        case .cardioDance:                  return "Cardio Dance"
        case .socialDance:                  return "Social Dance"
        case .pickleball:                   return "Pickleball"
        case .cooldown:                     return "Cooldown"

        // - Other
        case .other:                        return "Other"
        @unknown default:                   return "Other"
        
    

乔治·格林回答的实际版本

【讨论】:

您是否只是将 George Green 的答案复制到您自己的答案中?我很困惑这有什么不同。 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于如何在 HealthKit 中获取 HKWorkoutActivityType 的名称?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 HealthKit 中获取 HKWorkoutActivityType 的名称?

如何从 HealthKit 数据中获取最新的体重条目

如何从 HealthKit 数据中获取最新的体重条目

如何使用 HealthKit 从 iHealth 获取元数据?

如何使用 HealthKit 从 iHealth 获取元数据?

如何快速从 HealthKit 中获取氧饱和度?