Class

HKUnit

Use the HKUnit class to manage units of measure within HealthKit.

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.

Subclassing Notes

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 HKTimeUnit 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.

Topics

Working with Units

init(from: String)

Returns the unit instance described by the provided string.

var unitString: String

A string representation of the unit object.

func isNull()

Returns a Boolean value indicating whether the unit is null.

Working with Formatter Units

class func energyFormatterUnit(from: HKUnit)

Converts a HealthKit unit object into a corresponding energy formatter enumeration value.

init(from: EnergyFormatter.Unit)

Converts an energy formatter enumeration value into a corresponding HealthKit unit object.

class func lengthFormatterUnit(from: HKUnit)

Converts a HealthKit unit object into a corresponding length formatter enumeration value.

init(from: LengthFormatter.Unit)

Converts a length formatter enumeration value into a corresponding HealthKit object.

class func massFormatterUnit(from: HKUnit)

Converts a HealthKit unit object into a corresponding mass formatter enumeration value.

init(from: MassFormatter.Unit)

Converts a mass formatter enumeration value into a corresponding HealthKit unit object.

Constructing Mass Units

class func gram()

Returns a HealthKit unit for measuring mass in grams.

class func gramUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring mass, using gram units with the provided prefix.

class func ounce()

Returns a HealthKit unit for measuring mass in ounces.

class func pound()

Returns a HealthKit unit for measuring mass in pounds.

class func stone()

Returns a HealthKit unit for measuring mass in stones.

class func moleUnit(withMolarMass: Double)

Returns a HealthKit unit for measuring mass in moles for a given molar mass.

class func moleUnit(with: HKMetricPrefix, molarMass: Double)

Returns a HealthKit unit for measuring mass in moles, with the given prefix and molar mass.

Constructing Length Units

class func meter()

Returns a HealthKit unit for measuring length in meters.

class func meterUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring length, using meter units with the provided prefix.

class func inch()

Returns a HealthKit unit for measuring length in inches.

class func foot()

Returns a HealthKit unit for measuring length in feet.

class func yard()

Returns a HealthKit unit for measuring length in yards.

class func mile()

Returns a HealthKit unit for measuring length in miles.

Constructing Volume Units

class func liter()

Returns a HealthKit unit for measuring volume in liters.

class func literUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring volume, using liter units with the provided prefix.

class func fluidOunceUS()

Returns a HealthKit unit for measuring volume in US fluid ounces.

class func fluidOunceImperial()

Returns a HealthKit unit for measuring volume in imperial fluid ounces.

class func cupUS()

Returns a HealthKit unit for measuring volume in US cups.

class func cupImperial()

Returns a HealthKit unit for measuring volume in imperial cups.

class func pintUS()

Returns a HealthKit unit for measuring volume in US pints.

class func pintImperial()

Returns a HealthKit unit for measuring volume in imperial pints.

Constructing Pressure Units

class func pascal()

Returns a HealthKit unit for measuring pressure in pascals.

class func pascalUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring pressure, using pascal units with the provided prefix.

class func millimeterOfMercury()

Returns a HealthKit unit for measuring pressure in millimeters of mercury.

class func centimeterOfWater()

Returns a HealthKit unit for measuring pressure in centimeters of water.

class func atmosphere()

Returns a HealthKit unit for measuring pressure in atmospheres.

Constructing Time Units

class func second()

Returns a HealthKit unit for measuring time in seconds.

class func secondUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring time, using second units with the provided prefix.

class func minute()

Returns a HealthKit unit for measuring time in minutes.

class func hour()

Returns a HealthKit unit for measuring time in hours.

class func day()

Returns a HealthKit unit for measuring time in days.

Constructing Energy Units

class func joule()

Returns a HealthKit unit for measuring energy in joules.

class func jouleUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring energy, using joule units with the provided prefix.

class func calorie()

Returns a HealthKit unit for measuring energy in calories.

Deprecated
class func kilocalorie()

Returns a HealthKit unit for measuring energy in kilocalories.

class func largeCalorie()

Returns a HealthKit unit for measuring energy in large calories (Cal).

class func smallCalorie()

Returns a HealthKit unit for measuring energy in large calories (cal).

Constructing Temperature Units

class func degreeCelsius()

Returns a HealthKit unit for measuring temperature in degrees Celsius.

class func degreeFahrenheit()

Returns a HealthKit unit for measuring temperature in degrees Fahrenheit.

class func kelvin()

Returns a HealthKit unit for measuring temperature in kelvins.

Constructing Electrical Conductance Units

class func siemen()

Returns a HealthKit unit for measuring electrical conductance in siemens.

class func siemenUnit(with: HKMetricPrefix)

Returns a HealthKit unit for measuring electrical conductance, using siemen units with the provided prefix.

Constructing Pharmacology Units

class func internationalUnit()

Returns a HealthKit unit that measures the amount of a biologically active substance in international units (IU).

Constructing Scalar Units

class func count()

Returns a HealthKit unit for measuring counts.

class func percent()

Returns a HealthKit unit for measuring percentages.

Performing Unit Math

func unitMultiplied(by: HKUnit)

Creates a complex unit by multiplying the receiving unit with another unit.

func unitDivided(by: HKUnit)

Creates a complex unit by dividing the receiving unit by another unit.

func unitRaised(toPower: Int)

Creates a complex unit by raising the unit to the given power.

func reciprocal()

Returns a complex unit representing the unit’s reciprocal.

Constants

Molecular Masses

Constants used to create specific molecular mass units.

enum HKMetricPrefix

Prefixes that can be added to SI units to change the unit’s order of magnitude.

Relationships

Inherits From