Class

HKCategorySample

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

Overview

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

Topics

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

Constants 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.

Predicate Key Paths

let HKPredicateKeyPathCategoryValue: String

The key path for the category sample’s value.

Relationships

Inherits From

Conforms To

See Also

First Steps

Saving Data to HealthKit

Create and share HealthKit samples.

class HKQuantitySample

A sample that represents the quantity (both the value and units) of a single measurement.

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.