如何获取HealthKit中HKWorkoutActivityType的名称?

问题描述 投票:0回答:4

好的,所以

workoutActivityType
的属性
HKWorkout
返回
NSInteger

这里是活动列表:workoutActivityType 列表

现在,我将创建一个

plist
,并在获得
integer
时查找活动名称。但必须经历这一切似乎有点奇怪。

我是否需要查明这是我自己的活动,还是我遗漏了什么?

ios objective-c healthkit
4个回答
22
投票

对于现在仍然遇到此问题的任何人,我已经在 swift 中组合了一个简单的

HKWorkoutActivityType
扩展,以允许将这些类型映射到用户友好的字符串,以及可用的适当的表情符号表示:https://github.com/ georgegreenoflondon/HKWorkoutActivityType-Descriptions/.

这个问题的相关片段是:

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"
        }
    }

}

5
投票

不幸的是,苹果只提供了一个枚举,你必须实现你自己的名称。一个好主意是延迟实例化一个包含

NSNumber
键(对象包装
NSInteger
)和字符串值的字典。应该很容易制作,不幸的是唯一的解决方案。


3
投票
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"
        }
    }
}

乔治·格林针对 iOS 14 的答案的实际版本


0
投票

您可以编写自己的解析器,从本页获取

string
https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype

我们应该编写生成的代码,并在 iOS 应用程序中运行它。这样,

rawValue
HKWorkoutActivityType
始终是正确且最新的

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应用程序中运行生成的代码,我们可以得到

(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"
© www.soinside.com 2019 - 2024. All rights reserved.