Type Method

predicateForCategorySamples(with:value:)

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

Declaration

class func predicateForCategorySamples(with operatorType: NSComparisonPredicate.Operator, value: Int) -> NSPredicate

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 NSComparisonPredicate.Operator.contains.

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

let asleep = HKQuery.predicateForCategorySamplesWithOperatorType(
    .EqualToPredicateOperatorType,
    value: HKCategoryValueSleepAnalysis.Asleep.rawValue)
 
let explicitAsleep =
    NSPredicate(format: "%K == %d",
                HKPredicateKeyPathCategoryValue,
                HKCategoryValueSleepAnalysis.Asleep.rawValue)

See Also