<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKQuery/predicateForObjects(withMetadataKey:allowedValues:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKQuery(cm)predicateForObjectsWithMetadataKey:allowedValues:"
  },
  "title" : "predicateForObjects(withMetadataKey:allowedValues:)"
}
-->

# predicateForObjects(withMetadataKey:allowedValues:)

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

```
class func predicateForObjects(withMetadataKey key: String, allowedValues: [Any]) -> NSPredicate
```

## 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 <doc://com.apple.documentation/documentation/Foundation/NSString>, <doc://com.apple.documentation/documentation/Foundation/NSNumber>, or <doc://com.apple.documentation/documentation/Foundation/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 <doc://com.apple.documentation/documentation/Swift/true>; otherwise, it returns <doc://com.apple.documentation/documentation/Swift/false>.

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

```swift
let metadataValues =
    HKQuery.predicateForObjectsWithMetadataKey(HKMetadataKeyFoodType,
                                               allowedValues: ["Pizza", "Tofu"])
 
 
let explicitMetadataKey = NSPredicate(format: "%K.%K IN %@",
                                      HKPredicateKeyPathMetadata, HKMetadataKeyFoodType, ["Pizza", "Tofu"])
```

## See Also

[`HKPredicateKeyPathMetadata`](/documentation/HealthKit/HKPredicateKeyPathMetadata)

The key path for accessing the object’s metadata dictionary inside a predicate format string.

[`metadata`](/documentation/HealthKit/HKObject/metadata)

The metadata for this HealthKit object.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)