A sample whose values are selected from a short list of possible values.


class HKCategorySample : HKSample


For example, you can use category samples to record sleep data using the HKCategoryValueSleepAnalysis enumeration. Individual samples represent time periods when the user is in bed or asleep. Samples with different values may have overlapping time intervals. For example, when the user is both in bed and asleep, you would create an in-bed sample and an asleep sample with overlapping times.

The HKCategorySample class is a concrete subclass of the HKSample class. Category samples are immutable: The sample’s properties are set when the object is first created, and they cannot change.

Using Category Samples

Like many HealthKit classes, the HKCategorySample class should not be subclassed.

You may extend the HKCategorySample class by adding metadata with custom keys as appropriate for your app. For more information, see init(type:value:start:end:metadata:).


Creating Category Samples

init(type: HKCategoryType, value: Int, start: Date, end: Date)

Creates a newly instantiated category sample.

init(type: HKCategoryType, value: Int, start: Date, end: Date, metadata: [String : Any]?)

Creates a newly instantiated category sample with the provided metadata.

init(type: HKCategoryType, value: Int, start: Date, end: Date, device: HKDevice?, metadata: [String : Any]?)

Creates a newly instantiated category sample including the provided device and metadata.

Getting Property Data

var categoryType: HKCategoryType

The category type for this sample.

var value: Int

The category value for this sample.

Valid Values

enum HKCategoryValue

Miscellaneous category values.

enum HKCategoryValueCervicalMucusQuality

Categories used to represent the user’s cervical mucus quality.

enum HKCategoryValueMenstrualFlow

Categories indicating the amount of menstrual flow for a given sample.

enum HKCategoryValueOvulationTestResult

Categories used to represent the result of an ovulation home test.

enum HKCategoryValueSleepAnalysis

Categories used when analyzing sleep samples.

enum HKCategoryValueAudioExposureEvent

Categories indicating audio exposure events.


Predicate Key Paths

let HKPredicateKeyPathCategoryValue: String

The key path for the category sample’s value.


Inherits From

Conforms To

See Also

First Steps

Saving Data to HealthKit

Create and share HealthKit samples.

Reading and Writing HealthKit Series Data

Share and read heartbeat and quantity series data using series builders and queries.

class HKQuantitySample

A sample that represents a quantity, including the value and the units.

class HKCorrelation

A sample that groups multiple related samples into a single entry.

Units and Quantities

Objects used to specify a quantity for a given unit (for example, 175 lbs), and to convert between units (79.4 kg).

Metadata Keys

Constants used to add metadata to objects stored in HealthKit.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software