How to get the name HKWorkoutActivityType in HealthKit?

So, the workoutActivityType property for HKWorkout returns a NSInteger .

Here is a list of actions: workoutActivityType list

I am currently plist and will look for the name of the activity when I get the integer . But it seems a little strange to get through it.

Do I need to find out what activity it belongs to me or is there something that I do not see?

+7
source share
3 answers

Unfortunately, apple only provides an enumeration, and you have to implement your own names. A good idea is to lazily create a dictionary that includes NSNumber keys (Object wrapped NSInteger ) and string values. It should be fairly easy to make and, unfortunately, the only solution.

+3
source

You can simply write your own parser, take a string from this page https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype

We must write the generated code and run it in the iOS application. So rawValue HKWorkoutActivityType always correct and updated

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

Run the generated code in the iOS application, we can get

 (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" 
0
source

for Swift, I renamed this dictionary from the corresponding header with an additional field that indicates whether the activity type is deprecated: https://gist.github.com/MrMatthias/acc3f1ded4291bff20642f6083ae1842

0
source

Source: https://habr.com/ru/post/986991/


All Articles