A class for managing the units of measure within HealthKit.
SDKs
- iOS 8.0+
- watchOS 2.0+
Framework
- Health
Kit
Declaration
class HKUnit : NSObject
Overview
The unit class supports most standard SI units (meters, seconds, and grams), SI units with prefixes (centimeters, milliseconds and kilograms) and equivalent non-SI units (feet, minutes, and pounds). HealthKit also supports creating complex units by mathematically combining existing units.
You use units when working with HealthKit quantities. Quantities store both the value (as a double
data type) and its corresponding unit. You can then request the value from the quantity in any compatible units. For more information on working with quantities, see HKQuantity
.
Note
Number formatters that use units (for example, Energy
, Length
, and Mass
) use a custom enumeration to specify their units. For example, the Energy
class uses the Energy
enum. The HKUnit
class provides several methods to translate between the formatter enumerations and the HealthKit units. For more information, see Working with Formatter Units.
Using Units
Like many HealthKit classes, the HKUnit
class is not extendable and should not be subclassed.
The HKUnit
class is implemented using a facade design pattern. It uses custom subclasses to represent instances of the different unit types. For example, the second()
convenience method actually returns an instance of the private HKTime
subclass.
Additionally, the unit class uses a single unit instance to represent all copies of the same unit in your app, wherever possible. For example, two calls to the second()
method return the same unit object. This helps reduce the amount of memory used by unit instances.