<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKCorrelation/init(type:start:end:objects:metadata:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKCorrelation(cm)correlationWithType:startDate:endDate:objects:metadata:"
  },
  "title" : "init(type:start:end:objects:metadata:)"
}
-->

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

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

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

## Parameters

`correlationType`

The type for this correlation. For a complete list of correlation types, see Correlation Types.

`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 (<doc://com.apple.documentation/documentation/Foundation/NSExceptionName/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 (<doc://com.apple.documentation/documentation/Foundation/NSExceptionName/invalidArgumentException>).

`objects`

A set of [`HKSample`](/documentation/HealthKit/HKSample) objects. Specifically, this set contains the quantity and category samples to be grouped into this correlation.

`metadata`

The metadata dictionary containing extra information that describes this correlation. The dictionary’s keys are all <doc://com.apple.documentation/documentation/Foundation/NSString> objects . The values may be <doc://com.apple.documentation/documentation/Foundation/NSString>, <doc://com.apple.documentation/documentation/Foundation/NSNumber>, or <doc://com.apple.documentation/documentation/Foundation/NSDate> objects. For a complete list of predefined metadata keys, see <doc://com.apple.healthkit/documentation/HealthKit/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`](/documentation/HealthKit/HKMetadataKeyFoodType)     key to provide the food’s name.

## 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, and metadata as shown below.

Use this method when you need to include additional metadata, but the data was not recorded using external hardware. Samples representing food should always include metadata with the [`HKMetadataKeyFoodType`](/documentation/HealthKit/HKMetadataKeyFoodType) key.

```swift
let date = NSDate();
 
// Create a sample for calories
 
guard let calorieType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed) else {
    fatalError("*** Unable to create the calorie type ***")
}
 
let calorieQuantity =
    HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: 110.0)
 
let calorieSample = HKQuantitySample(type: calorieType,
                                     quantity: calorieQuantity, startDate: date, endDate: date)
 
// Create a sample for total fat
 
guard let fatType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryFatTotal) else {
    fatalError("*** Unable to create the fat type ***")
}
 
let fatQuantity =
    HKQuantity(unit: HKUnit.gramUnit(), doubleValue: 0.0)
 
let fatSample = HKQuantitySample(type: fatType,
                                 quantity: fatQuantity, startDate: date, endDate: date)
 
// Create a sample for carbohydrates
 
guard let carbohydratesType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryCarbohydrates) else {
    fatalError("*** Unable to create the carbohydrates type ***")
}
 
let carbohydratesQuantity =
    HKQuantity(unit: HKUnit.gramUnit(), doubleValue: 30.0)
 
let carbohydratesSample = HKQuantitySample(type: carbohydratesType,
                                           quantity: carbohydratesQuantity, startDate: date, endDate: date)
 
// Create a sample for protein
 
guard let proteinType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryProtein) else {
    fatalError("*** Unable to create the protein type ***")
}
 
let proteinQuantity =
    HKQuantity(unit: HKUnit.gramUnit(), doubleValue: 1.0)
 
let proteinSample = HKQuantitySample(type: proteinType,
                                     quantity: proteinQuantity, startDate: date, endDate: date)
 
// Create the food sample
 
let objects: Set = [calorieSample, fatSample, carbohydratesSample, proteinSample]
 
let metadata = [HKMetadataKeyFoodType: "Banana"]
 
guard let bananaType = HKObjectType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierFood) else {
    fatalError("*** Unable to create the banana type ***")
}
 
let banana = HKCorrelation(type: bananaType,
                           startDate: date, endDate: date, objects: objects, metadata:metadata)
```

## See Also

[`objects`](/documentation/HealthKit/HKCorrelation/objects)

The set of sample objects that make up the correlation.

[`+  correlationTypeForIdentifier:`](/documentation/HealthKit/HKObjectType/correlationType(forIdentifier:))

Returns the shared correlation type for the provided identifier.

[`endDate`](/documentation/HealthKit/HKSample/endDate)

The sample’s end date.

[`correlationType`](/documentation/HealthKit/HKCorrelation/correlationType)

The type for this correlation.

[`metadata`](/documentation/HealthKit/HKObject/metadata)

The metadata for this HealthKit object.

[`startDate`](/documentation/HealthKit/HKSample/startDate)

The sample’s start date.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)