Contents

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

Creates a newly instantiated category sample with the provided metadata.

Declaration

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

Parameters

  • type:

    The category type for this sample. For a complete list, see 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 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).

  • metadata:

    The metadata dictionary contains extra information describing this sample. The dictionary’s keys are all Nsstring objects. The values may be Nsstring objects, Nsnumber objects or Nsdate objects. For a complete list of predefined metadata keys, see Metadata Keys in Healthkit Constants.

    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 category sample’s capabilities.

Return Value

A valid category sample with metadata.

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 date, end date, and metadata, as shown below.

See Also

Related Documentation

Creating Category Samples