Contents

init(type:start:end:objects:device:metadata:)

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

Declaration

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

Parameters

  • correlationType:

    The type for this correlation. For a complete list of correlation type identifiers, see Hkcorrelationtypeidentifier.

  • startDate:

    The start date for the sample. This date must be equal to or earlier than the end date; otherwise, this method throws an exception (Invalidargumentexception).

  • endDate:

    The end date for the sample. This date must be equal to or later than the start date; otherwise, this method throws an exception (Invalidargumentexception).

  • objects:

    A set of Hksample objects. Specifically, this set contains the quantity and category samples to be grouped into this correlation.

  • device:

    The device that generated the data for this sample.

  • metadata:

    The metadata dictionary containing extra information that describes this correlation. The dictionary’s keys are all Nsstring objects . The values may be Nsstring, Nsnumber, or Nsdate objects. For a complete list of predefined metadata keys, see Metadata Keys.

    Using predefined keys helps facilitate sharing data between apps; however, you are also encouraged to create custom keys as needed to extend the HealthKit sample’s capabilities.

    When creating correlations representing food, always use the Hkmetadatakeyfoodtype key to provide the food’s name.

Return Value

A new correlation instance.

Discussion

Use a correlation object to represent composite data—that is, a sample that requires more than a single value. To create a correlation sample, first create the quantity and category samples you intend to combine into the correlation. Next, create the correlation’s type. Finally, instantiate the correlation, passing in the type, start date, end date, samples, device, and metadata as shown below.

Use this method when recording data using external hardware. If you do not need to include additional metadata, pass nil for the metadata parameter.

See Also

Related Documentation

Creating Correlations