Class

HKQuantitySample

A sample that represents a quantity, including the value and the units.

Declaration

class HKQuantitySample : HKSample

Overview

A quantity sample contains one or more HKQuantity objects. Each quantity represents a single piece of data with a single numeric value and the value’s associated units. For example, you can use quantity samples to record the user’s height, the user’s current heart rate, or the number of calories in a hamburger. HealthKit provides a wide range of quantity types, letting you track many different health and fitness features.

The HKQuantitySample class is a subclass of the HKSample class. Quantity samples are immutable; you set the sample’s properties when you create it, and they cannot change.

In iOS 13 and later and watchOS 6 and later, HKQuantitySample is an abstract superclass for the HKCumulativeQuantitySample and HKDiscreteQuantitySample concrete subclasses. The system automatically selects the correct subclass based on the HKQuantityType object used to create the sample.

Use Quantity Samples

Like many HealthKit classes, you should not subclass the HKQuantitySample class. You may extend this class by adding metadata with custom keys to save related data used by your app. For more information, see init(type:quantity:start:end:metadata:).

Topics

Creating Quantity Samples

init(type: HKQuantityType, quantity: HKQuantity, start: Date, end: Date)

Returns a sample containing a numeric measurement.

init(type: HKQuantityType, quantity: HKQuantity, start: Date, end: Date, metadata: [String : Any]?)

Returns a sample containing a numeric measurement with the provided metadata.

init(type: HKQuantityType, quantity: HKQuantity, start: Date, end: Date, device: HKDevice?, metadata: [String : Any]?)

Returns a sample containing a numeric measurement with the provided device and metadata.

Getting Property Data

var quantity: HKQuantity

The quantity for this sample.

var count: Int

The number of quantities contained in this sample.

var quantityType: HKQuantityType

The quantity type for this sample.

Predicate Key Paths

let HKPredicateKeyPathQuantity: String

The key path to the sample’s quantity.

let HKPredicateKeyPathCount: String

A key path for the sample’s count.

Beta

Relationships

Inherits From

Conforms To

See Also

First Steps

Saving Data to HealthKit

Create and share HealthKit samples.

Reading and Writing HealthKit Series Data

Share and read heartbeat and quantity series data using series builders and queries.

class HKCategorySample

A sample whose values are selected from a short list of possible values.

class HKCorrelation

A sample that groups multiple related samples into a single entry.

Units and Quantities

Objects used to specify a quantity for a given unit (for example, 175 lbs), and to convert between units (79.4 kg).

Metadata Keys

Constants used to add metadata to objects stored in HealthKit.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software