Class

HKCorrelation

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

Declaration

class HKCorrelation : HKSample

Overview

HealthKit uses correlations to represent both blood pressure and food.

  • Blood pressure correlations always include two quantity samples, representing the systolic and diastolic values.

  • Food correlations can contain a wide range of dietary information about the food, including information about the fat, protein, carbohydrates, energy, and vitamins consumed.

In general, a food correlation should include at least a dietaryEnergyConsumed sample. You can also add nutritional quantity samples for any other items you want to track. Use the HKMetadataKeyFoodType key to indicate the food’s name.

The HKCorrelation class is a concrete subclass of the HKSample class. Correlations are immutable: You set the correlation’s properties when the object is first created, and they cannot change.

Using Correlation Samples

Like many HealthKit classes, the HKCorrelation class should not be subclassed. You can extend the correlation class by adding metadata with custom keys as appropriate for your app. For more information, see the init(type:start:end:objects:metadata:) method.

Topics

Creating Correlations

init(type: HKCorrelationType, start: Date, end: Date, objects: Set<HKSample>)

Instantiates and returns a new correlation instance.

init(type: HKCorrelationType, start: Date, end: Date, objects: Set<HKSample>, metadata: [String : Any]?)

Instantiates and returns a new correlation instance with the provided metadata.

init(type: HKCorrelationType, start: Date, end: Date, objects: Set<HKSample>, device: HKDevice?, metadata: [String : Any]?)

Instantiates and returns a new correlation instance with the provided device and metadata.

Getting Correlation Data

var correlationType: HKCorrelationType

The type for this correlation.

var objects: Set<HKSample>

The set of sample objects that make up the correlation.

func objects(for: HKObjectType) -> Set<HKSample>

Returns a set containing all the objects of the specified type in the correlation.

Predicate Key Paths

let HKPredicateKeyPathCorrelation: String

The key path for accessing the object’s correlation inside a predicate format string.

Relationships

Inherits From

Conforms To

See Also

First Steps

Saving Data to HealthKit

Create and share HealthKit samples.

class HKQuantitySample

A sample that represents the quantity (both the value and units) of a single measurement.

class HKCategorySample

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

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.