init(from:)
Returns the unit instance described by the provided string.
Declaration
convenience init(from string: String)Parameters
- string:
A string representation of the unit, such as
count,kg, orm/s^2.
Return Value
The unit object described by the string. If the string does not represent a valid unit, this method throws an exception (invalidArgumentException).
Discussion
You can create unit objects using a string representation of that unit, which is a convenient way to create complex, compound units. A few sample units are shown below.
Unit strings use one or more characters to represent the International System of Units (SI). The unit strings for SI units are listed in the following table.
String | Unit name | Unit type |
|---|---|---|
| Grams | Mass |
| Meters | Length |
| Liters | Volume |
| Pascals | Pressure |
| Seconds | Time |
| Joules | Energy |
| Kelvin | Temperature |
| Siemens | Electrical conductance |
| Moles | Mass |
For molar mass, you must include the molar mass (in g/mol) of the item to be weighed. For example, for blood glucose samples, use mol<180.15588000005408>.
Each SI unit can also take an optional prefix. Valid prefix strings are listed in the following table.
Prefix | Name | Multiplier |
|---|---|---|
| Pico- | 1.0e-12 |
| Nano- | 1.0e-9 |
| Micro- | 1.0e-6 |
| Milli- | 0.001 |
| Centi- | 0.01 |
| Deci- | 0.1 |
| Deca- | 10 |
| Hecto- | 100 |
| Kilo- | 1000 |
| Mega- | 1.0e6 |
| Giga- | 1.0e9 |
| Tera- | 1.0e12 |
The HKUnit class also supports a number of non-SI units, as shown in the following table.
String | Unit name | Unit type |
|---|---|---|
| Ounces | Mass |
| Pounds | Mass |
| Stones | Mass |
| Inches | Length |
| Feet | Length |
| Yard | Length |
| Miles | Length |
| Millimeters of mercury | Pressure |
| Inches of mercury | Pressure |
| Centimeters of water | Pressure |
| Atmospheres | Pressure |
| U.S. fluid ounces | Volume |
| Imperial fluid ounces | Volume |
| U.S. cup | Volume |
| Imperial cup | Volume |
| U.S. pint | Volume |
| Imperial pint | Volume |
| Minutes | Time |
| Hours | Time |
| Days | Time |
| Hertz | Frequency |
| Small calories | Energy |
| Large calories | Energy |
| Kilocalories | Energy |
| Degrees Celsius | Temperature |
| Degrees Fahrenheit | Temperature |
| Decibel: Sound Pressure Level | Sound pressure |
| Decibel: Hearing Level | Sound pressure |
| International Unit | Dosage |
| Count | Count |
| Percent | Percent |
Of these, the count and percent units deserve special attention. Count units represent raw scalar values. They can be used to represent the number of times an event occurs—for example, the number of steps the user has taken or the number of times the user has used their inhaler. They can also be used as part of a compound unit—for example, the beats portion of beats per minute.
The percent unit measures a value between 0.0 and 1.0. HealthKit uses percent units when measuring body fat percentage, oxygen saturation, blood alcohol content, and similar values.
You can create more complex, compound units by mathematically combining multiple individual units. Unit strings can include symbols that indicate multiplication (. or *) or division (/), or raise a unit to a power (^). In compound unit strings, multiplication is evaluated before anything else. Additionally, there can only be one division symbol in the string. Common compound units are shown in the following table.
String | Unit name | Unit type | Sample type |
|---|---|---|---|
| Beats per minute | Count/time | Heart rate |
| Milligrams per deciliter | Mass/volume | Blood glucose level |
| Milliliters per kilogram per minute | Volume/mass/time | VO2 Max |
| Liters per minute | Volume/time | Peak Expiratory Flow Rate |