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

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

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

```
convenience init(type quantityType: HKQuantityType, quantity: HKQuantity, start startDate: Date, end endDate: Date, 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>).

`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 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, and metadata. You produce a new quantity sample with the provided metadata.

```objc
NSDictionary *metadata =
@{HKMetadataKeyDigitalSignature:digitalSignature,
HKMetadataKeyTimeZone:timeZone};
 
HKQuantityType *quantityType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
 
HKUnit *bpm = [HKUnit unitFromString:@"count/min"];
 
HKQuantity *quantity = [HKQuantity quantityWithUnit:bpm
                                        doubleValue:72.0];
 
HKQuantitySample *sample =
[HKQuantitySample quantitySampleWithType:quantityType
                                quantity:quantity
                               startDate:start
                                 endDate:end
                                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)