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

# init(type:value:start:end:)

Creates a newly instantiated category sample.

```
convenience init(type: HKCategoryType, value: Int, start startDate: Date, end endDate: Date)
```

## Parameters

`type`

The category type for this sample. For a complete list, see [`HKCategoryTypeIdentifier`](/documentation/HealthKit/HKCategoryTypeIdentifier).

`value`

The value for this sample. This value must come from the appropriate category value enumeration. Each category type uses its own enumeration. For more information, see [`HKCategoryTypeIdentifier`](/documentation/HealthKit/HKCategoryTypeIdentifier).

`startDate`

The start date for the sample. This 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 must be equal to or later than the start date; otherwise, this method throws an exception (<doc://com.apple.documentation/documentation/Foundation/NSExceptionName/invalidArgumentException>).

## Return Value

A valid category sample.

## Discussion

HealthKit uses category samples to represent data that can be classified into a finite set of categories. To create a category sample, you must first create the corresponding category type, and then set its start and end dates, as shown below.

```swift
guard let categoryType =
    HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierSleepAnalysis) else {
        fatalError("*** Unable to create a sleep analysis category type ***")
}
 
let categorySample = HKCategorySample(type: categoryType,
                                      value: HKCategoryValueSleepAnalysis.Asleep.rawValue,
                                      startDate: start,
                                      endDate: end)
```

## See Also

[`value`](/documentation/HealthKit/HKCategorySample/value)

The category value for this sample.

[`+  categoryTypeForIdentifier:`](/documentation/HealthKit/HKObjectType/categoryType(forIdentifier:))

Returns the shared category type for the provided identifier.

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

The sample’s end date.

[`categoryType`](/documentation/HealthKit/HKCategorySample/categoryType)

The category type for this sample.

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