Field notes
Reading a HealthKit Type Identifier in an Export Record
A HealthKit type identifier names what kind of measurement a record holds. Read its family, match it to Apple's documented name and avoid guessing.
What a HealthKit type identifier is#
A HealthKit type identifier is a string that names the kind of data a record holds. Read it first, before the value. It tells you whether the row is a number with a unit or a labeled state, and it points you to a page where Apple describes the type.
That matters when you keep your own log and later compare it with data pulled from Apple Health. A value without a type is a number with no meaning. A value with a type you have actually looked up is something you can audit months later.
This article covers the two families of identifier, the naming pattern that links a prefixed string to Apple's documentation, and the limits of what you can safely assume.
Two families: quantity and category identifiers#
Apple defines the identifier as a Swift struct for each family of type. The two you will meet most often are these.
Quantity identifiers. Apple describes HKQuantityTypeIdentifier as "the identifiers that create quantity type objects." Examples on that page include stepCount, heartRate, restingHeartRate, bodyMass and dietaryEnergyConsumed. These are measurements.
Category identifiers. Apple describes HKCategoryTypeIdentifier as "identifiers for creating category types." Examples include sleepAnalysis, appleStandHour, mindfulSession, handwashingEvent and lowHeartRateEvent. These are states or events.
Both structs conform to RawRepresentable and offer init(rawValue: String). In plain terms, every identifier is backed by a string. That string is what you see when a record is written out as text.
Both families sit under a common parent. Apple documents HKObjectType as an abstract superclass, with quantity and category types among its subclasses, next to correlation, characteristic and workout types. So the identifier does not stand alone. It selects one type inside one of those subclasses.
From prefixed string to documented name#
In Swift code, the cases are written in lowerCamelCase. The step count case is stepCount, and the sleep case is sleepAnalysis.
Apple's own pages also carry a longer symbol name. The stepCount page lists its precise identifier as HKQuantityTypeIdentifierStepCount. The sleepAnalysis page lists HKCategoryTypeIdentifierSleepAnalysis.
The pattern is the same in both cases:
- The family prefix,
HKQuantityTypeIdentifierorHKCategoryTypeIdentifier. - Then the case name with its first letter capitalized.
So if you meet a string that starts with one of those prefixes, you can work back to the documented name in three steps.
- Read the prefix to get the family.
- Drop the prefix and lowercase the first letter of what remains.
- Look that name up on the family's page.
HKQuantityTypeIdentifierStepCount becomes stepCount, which Apple describes as "a quantity sample type that measures the number of steps the user has taken." HKCategoryTypeIdentifierSleepAnalysis becomes sleepAnalysis, "a category sample type for sleep analysis information."
Do the lookup on every new string, and don't assume the name tells you everything. A name such as appleStandHour is a category, not a count of hours, and only Apple's page says what it records.
Quantity or category: the family tells you the value shape#
The family is your first clue to how to read the value next to it.
A quantity type carries a number, and that number only means something with its unit. A category type carries one of a fixed set of states or an event marker. You would not average category values the way you average a quantity, and you would not read a quantity as a label.
Units add their own traps. Two units that look alike can still fail to convert, and count and percent look alike in Apple Health but never convert. Check the unit every time you check the type.
A practical consequence for a personal log: keep type and unit as separate columns. When a type is a quantity, record its unit next to it. When it is a category, record the state label as text. Mixing the two in one column makes later sorting and charting harder than it needs to be.
What Apple documents and what it does not#
Apple's HealthKit pages describe the identifiers, their families and the Swift types that use them. In the pages used for this article, none describes the layout of an exported file, and none shows a sample exported record.
So use the identifier pages as a dictionary and your own export as the text you are reading. The dictionary tells you what sleepAnalysis means. It won't tell you how a given file arranges its fields, which attributes sit beside the type, or how dates are written. Check those against the file in front of you. As one example of a detail that belongs to the file and not the entry, the time zone offset belongs to the export.
If an identifier in your file does not match a page you can find, note it as unknown. Don't guess at what it measures. A blank "type unverified" note is more honest than a confident wrong label, and it is easy to fill in later.
A habit for reading an unfamiliar identifier#
You don't need to memorize the list. A small routine is enough.
- Write down the exact string as it appears.
- Note the family from its prefix.
- Work back to the documented case name.
- Open Apple's page for that name and copy its one-line description into your notes.
- Record the unit if it is a quantity, or the allowed states if it is a category.
Over a few sessions this becomes a short lookup table of the types you actually see. It is a record about your record, and it keeps working if the file changes shape. If you are still deciding how to store the archive itself, see choosing a format for your log archive.
OptimizedTracker keeps the same discipline for entries you log yourself: a value, a unit and a clear label, so the record reads the same later as it does today.
Conclusion#
A HealthKit type identifier is a family prefix plus a documented case name. The family tells you whether you are reading a measurement or a state. Apple's identifier pages tell you what each name means. What those pages do not tell you, such as the layout of your exported file, you confirm from the file itself.
Read the type first, look it up, record the unit, and mark anything you cannot verify. That small habit is what makes an exported record something you can trust later.
Share
Related
01
