Type Method

categorySampleWithType:value:startDate:endDate:

Creates a newly instantiated category sample.

Declaration

+ (instancetype)categorySampleWithType:(HKCategoryType *)type value:(NSInteger)value startDate:(NSDate *)startDate endDate:(NSDate *)endDate;

Parameters

type

The category type for this sample. For a complete list, see Category Type Identifiers in HealthKit Constants.

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 Category Type Identifiers in HealthKit Constants.

startDate

The start date for the sample. This must be equal to or earlier than the end date; otherwise, this method throws an exception (NSInvalidArgumentException).

endDate

The end date for the sample. This must be equal to or later than the start date; otherwise, this method throws an exception (NSInvalidArgumentException).

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.

HKCategoryType *categoryType =
[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
 
HKCategorySample *categorySample =
[HKCategorySample categorySampleWithType:categoryType
                                   value:HKCategoryValueSleepAnalysisAsleep
                               startDate:start
                                 endDate:end];

See Also

Creating Category Samples

+ categorySampleWithType:value:startDate:endDate:metadata:

Creates a newly instantiated category sample with the provided metadata.

+ categorySampleWithType:value:startDate:endDate:device:metadata:

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