Type Method

quantitySampleWithType:quantity:startDate:endDate:

Returns a sample containing a numeric measurement.

Declaration

+ (instancetype)quantitySampleWithType:(HKQuantityType *)quantityType quantity:(HKQuantity *)quantity startDate:(NSDate *)startDate endDate:(NSDate *)endDate;

Parameters

quantityType

The type of sample to be created. HealthKit defines a number of different quantity types, representing different types of health and fitness data. For the complete list of quantity type identifiers, see HKQuantityTypeIdentifier.

quantity

The value to be stored in the sample. The quantity object must use units that are compatible with the provided quantity type. If the units are not compatible, this method throws an exception (NSInvalidArgumentException).

startDate

The start date for the sample. This date 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 date must be equal to or later than the start date; otherwise, this method throws an exception (NSInvalidArgumentException).

Return Value

A valid quantity sample.

Discussion

HealthKit uses quantity samples to represent sample data using a numeric value. To create a quantity sample, first create the corresponding quantity type and quantity, and then set its start and end dates. You produce a new quantity sample.

HKQuantityType *quantityType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
 
HKUnit *bpm = [HKUnit unitFromString:@"count/min"];
 
HKQuantity *quantity = [HKQuantity quantityWithUnit:bpm
                                        doubleValue:72.0];
 
HKQuantitySample *sample =
[HKQuantitySample quantitySampleWithType:quantityType
                                quantity:quantity
                               startDate:start
                                 endDate:end];

See Also

Creating Quantity Samples

+ quantitySampleWithType:quantity:startDate:endDate:metadata:

Returns a sample containing a numeric measurement with the provided metadata.

+ quantitySampleWithType:quantity:startDate:endDate:device:metadata:

Returns a sample containing a numeric measurement with the provided device and metadata.