Predicate
struct · iOS 17.0+ · SDK 27.1
struct Predicate<each Input>A logical condition used to test a set of input values for searching or filtering. A predicate is a logical condition that evaluates to a Boolean value (true or false). You use predicates for operations like filtering a collection or searching for matching elements. To create a predicate, use the `Predicate(_:)` macro. For example: ```swift let messagePredicate = #Predicate<Message> { message in message.length < 100 && message.sender == "Jeremy" } ``` In the example above, the closure that contains the predicate's conditions takes one argument — the value being tested. Even though you write the predicate using a closure, the macro transforms that closure into a predicate when you compile. The code in the closure isn't run as part of your program. In the predicate's definition, you can use the following operations: - Arithmetic (`+`, `-`, `*`, `/`, `%`) - Unary minus (`-`) - Range (`...`, `..<`) - Comparison (`<`, `<=`, `>`, `>=`, `==`, `!=`) - Ternary conditional (`?:`) - Conditional expressions - Boolean logic (`&&`, `||`, `!`) - Swift optionals (`?`, `??`, `!`, `flatMap(_:)`, `if`-`let` expressions) - Types (`as`, `as?`, `as!`, `is`) - Sequence operations (`allSatisfy()`, `filter()`, `contains()`, `contains(where:)`, `starts(with:)`, `max()`, `min()`) - Subscript and member access (`[]`, `.`) - String comparisons (`contains(_:)`, `localizedStandardContains(_:)`, `caseInsensitiveCompare(_:)`, `localizedCompare(_:)`) A predicate can't contain any nested declarations, use any flow control such as `for` loops, or modify variables from its enclosing scope. However, it can refer to constants that are in scope. To express more complex queries, you can nest expressions in the predicate: ```swift let messagePredicate = #Predicate<Message> { message in message.recipients.contains { $0.firstName == message.sender.firstName } } ``` You can safely encode and decode predicates, pass predicates across concurrency boundaries, and load a predicate from a file. To define a list of types and key paths that are allowed when reading an archived predicate, use ``PredicateCodableConfiguration``. You can transform a predicate into another representation — for example, to express a predicate in another query language, or to create a modified predicate — using the ``expression`` property.
Members (0)
This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.
Skipped declarations
9 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
Predicate.encode(to:configuration:)
Swift parameter packs cannot cross the bridge.
Predicate.init(from:configuration:)
Swift parameter packs cannot cross the bridge.
Predicate.variable
Swift parameter packs cannot cross the bridge.
Predicate.init(_:)
Swift parameter packs cannot cross the bridge.
Predicate.evaluate(_:)
Swift parameter packs cannot cross the bridge.
Predicate.true
Swift parameter packs cannot cross the bridge.
Predicate.false
Swift parameter packs cannot cross the bridge.
Predicate.init(all:)
This parameter hides a concrete Swift type.
Predicate.init(any:)
This parameter hides a concrete Swift type.