DEV Community

Khoa Pham
Khoa Pham

Posted on

1 1

Getting activity name through HKWorkoutActivityType in HealthKit

After fetching workouts with HKObjectType.workoutType() , we get HKWorkoutActivityType , which is an enum enum HKWorkoutActivityType : UInt . As of Swift 4.2, there are no way to get enum case as String because this enum has type UInt .

Though there will be some manual involved, we can build a generator to get all the enum case names. Here we will write code to generate code, and use that generated code to examine all the values.

Execute this Swift code in our iOS app

func makeCode(string: String) {
 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)\””)
 })
}

Where string is all the cases from HKWorkoutActivityType, for example

[case archery](https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype/archery)
The constant for shooting archery.

[case bowling](https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype/bowling)
The constant for bowling

[case fencing](https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype/fencing)
The constant for fencing.

[case gymnastics](https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype/gymnastics)
Performing gymnastics.

[case trackAndField](https://developer.apple.com/documentation/healthkit/hkworkoutactivitytype/trackandfield)
Participating in track and field events, including shot put, javelin, pole vaulting, and related sports.

What the code does is to use regular expression to examine all the names after case , and build our code

dictionary[HKWorkoutActivityType.americanFootball.rawValue] = “americanFootball”
dictionary[HKWorkoutActivityType.archery.rawValue] = “archery”
dictionary[HKWorkoutActivityType.australianFootball.rawValue] = “australianFootball”
dictionary[HKWorkoutActivityType.badminton.rawValue] = “badminton”
dictionary[HKWorkoutActivityType.baseball.rawValue] = “baseball”

The above generated code with dictionary contains rawValue as key and the enum case name as value .

Later when we get any HKWorkoutActivityType , we can compare with this dictionary to find the actual name. This is better than hardcode activity name with numbers because those rawValue are just implementation detail

Original post https://medium.com/fantageek/getting-activity-name-through-hkworkoutactivitytype-in-healthkit-51109e022c33

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs