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

Sentry mobile image

Tired of users complaining about slow app loading and janky UI?

Improve performance with key strategies like TTID/TTFD & app start analysis.

Read the blog post

Top comments (0)

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

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay