Type Method

predicateForObjectsWithMetadataKey:allowedValues:

Returns a predicate that matches objects based on the provided metadata key and an array of target values.

Declaration

+ (NSPredicate *)predicateForObjectsWithMetadataKey:(NSString *)key allowedValues:(NSArray *)allowedValues;

Parameters

key

The metadata key for the value to be matched. For a list of preset keys, see Metadata Keys. You may also search using custom keys.

allowedValues

An array of valid values. These values must be NSString, NSNumber, or NSDate instances.

Return Value

A predicate that matches objects based on the provided metadata key and an array of target values..

Discussion

Use this convenience method to create a predicate that matches objects based on their metadata. When this predicate is evaluated, it gets the metadata’s value for the provided key. Then the predicate checks that value against the allowedValues array. If the array contains a matching value, the predicate returns YES; otherwise, it returns NO.

The following sample uses both the convenience method and a predicate format string to create equivalent predicates.

NSPredicate *metadataValues =
    [HKQuery predicateForObjectsWithMetadataKey:HKMetadataKeyFoodType
                                  allowedValues:@[@"Pizza", @"Tofu"]];
 
NSPredicate *explicitMetadataValues =
    [NSPredicate predicateWithFormat:@"%K.%K in %@",
     HKPredicateKeyPathMetadata,
     HKMetadataKeyFoodType,
     @[@"Pizza", @"Tofu"]];

See Also

Creating Object Predicates

+ predicateForObjectWithUUID:

Returns a predicate that matches an object with the specified universally unique identifier (UUID).

+ predicateForObjectsWithUUIDs:

Returns a predicate that matches the objects with the specified universally unique identifiers (UUIDs).

+ predicateForObjectsFromSource:

Returns a predicate that matches all the objects that were created by the provided source.

+ predicateForObjectsFromSources:

Returns a predicate that matches all the objects that were created by any of the provided sources.

+ predicateForObjectsFromDevices:

Returns a predicate that matches all the objects that were created by any of the provided devices.

+ predicateForObjectsWithDeviceProperty:allowedValues:

Returns a predicate that matches all objects created by devices with the specified properties.

+ predicateForObjectsFromSourceRevisions:

Returns a predicate that matches all the objects that were created by any of the provided source revisions.

+ predicateForObjectsWithMetadataKey:

Returns a predicate that matches any object whose metadata contains the provided key.

+ predicateForObjectsWithMetadataKey:operatorType:value:

Returns a predicate that matches objects based on the provided metadata key, value, and operator.

+ predicateForObjectsWithNoCorrelation

Returns a predicate that matches all objects that are not associated with a HealthKit correlation.