Type Method

predicateForCategorySamplesWithOperatorType:value:

Returns a predicate that checks a category sample’s value.

Declaration

+ (NSPredicate *)predicateForCategorySamplesWithOperatorType:(NSPredicateOperatorType)operatorType value:(NSInteger)value;

Parameters

operatorType

The type of operation to perform when matching the category sample’s value against the target value. For a list of possible operators, see NSContainsPredicateOperatorType.

value

The category sample’s target value. Use an enumeration value appropriate for the type of category samples you are working with. For example, a predicate for sleep analysis samples should use values from the HKCategoryValueSleepAnalysis enumeration.

Return Value

A predicate that matches category samples based on the provided expression. This predicate works only with category samples.

Discussion

Use this convenience method to create a predicate that checks a category sample’s value. Listing 1 uses both the convenience method and a predicate format string to create equivalent predicates.

Listing 1

Matching category samples against a target value

NSPredicate *asleep =
    [HKQuery
     predicateForCategorySamplesWithOperatorType:NSEqualToPredicateOperatorType
     value:HKCategoryValueSleepAnalysisAsleep];
 
NSPredicate *explicitAsleep =
    [NSPredicate predicateWithFormat:@"%K == %d",
     HKPredicateKeyPathCategoryValue,
     HKCategoryValueSleepAnalysisAsleep];

See Also