<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.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/HKQuantitySample/init(type:quantity:start:end:device:metadata:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKQuantitySample(cm)quantitySampleWithType:quantity:startDate:endDate:device:metadata:"
  },
  "title" : "init(type:quantity:start:end:device:metadata:)"
}
-->

# init(type:quantity:start:end:device:metadata:)

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

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

## Parameters

`quantityType`

The type of sample to be created. HealthKit defines a number of different quantity types, representing different types of health and fitness data. For the complete list of quantity type identifiers, see [`HKQuantityTypeIdentifier`](/documentation/HealthKit/HKQuantityTypeIdentifier).

`quantity`

The value to be stored in the sample. The quantity object must use units that are compatible with the provided quantity type. If the units are not compatible, this method throws an exception (<doc://com.apple.documentation/documentation/Foundation/NSExceptionName/invalidArgumentException>).

`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>).

`device`

The device that generated the data for this sample.

`metadata`

The metadata dictionary contains extra information describing this sample. 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> objects, <doc://com.apple.documentation/documentation/Foundation/NSNumber> objects, 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 your own, custom keys as needed to extend the HealthKit quantity sample’s capabilities.

## Return Value

A valid quantity sample with the device and metadata.

## Discussion

HealthKit uses quantity samples to represent sample data using a single numeric value. To create a quantity sample, first create the corresponding quantity type and quantity, and then set its start date, end date, device, and metadata. You produce a new quantity sample with the provided device and metadata.

```swift
let device = HKDevice(name: deviceName,
                      manufacturer: manufacturerName,
                      model: modelName,
                      hardwareVersion: hardwareVersionNumber,
                      firmwareVersion: firmwareVersionNumber,
                      softwareVersion: softwareVersionNumber,
                      localIdentifier: localIdentifier,
                      UDIDeviceIdentifier: deviceIdentifier)
 
let metadata = [HKMetadataKeyDigitalSignature:digitalSignature,
                HKMetadataKeyTimeZone:timeZone]
 
guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else {
    fatalError("*** Unable to create a heart rate quantity type ***")
}
 
let bpm = HKUnit(fromString: "count/min")
let quantity = HKQuantity(unit: bpm, doubleValue: 72.0)
 
let quantitySample = HKQuantitySample(type: quantityType,
                                      quantity: quantity,
                                      startDate: start,
                                      endDate: end,
                                      device: device,
                                      metadata: metadata)
```

## See Also

[`quantityType`](/documentation/HealthKit/HKQuantitySample/quantityType)

The quantity type for this sample.

[`quantity`](/documentation/HealthKit/HKQuantitySample/quantity)

The quantity for this sample.

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

The sample’s end date.

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

The metadata for this HealthKit object.

[`+  quantityTypeForIdentifier:`](/documentation/HealthKit/HKObjectType/quantityType(forIdentifier:))

Returns the shared quantity type for the provided identifier.

[`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)