Predicate

public protocol Predicate : AsyncPredicate

The Predicate protocol defines the structure that must be implemented by concrete predicates.

public struct CopyCatPredicate: Predicate {

    private let value: String

    public init(value: String) {
        self.value = value
    }

    public func evaluate(with input: String) -> Bool {
        return input == value
    }
}

let predicate = CopyCatPredicate(value: "alphabet")
let isIdentical = predicate.evaluate(with: "alphabet")
  • A type that provides information about what kind of values the predicate can be evaluated with.

    Declaration

    Swift

    associatedtype InputType
  • Returns a Boolean value that indicates whether a given input matches the conditions specified by the receiver.

    Declaration

    Swift

    func evaluate(with input: InputType) -> Bool

    Parameters

    input

    The input against which to evaluate the receiver.

    Return Value

    true if input matches the conditions specified by the receiver, false otherwise.

  • erase() Extension method

    Wraps this predicate with an AnyPredicate.

    let odd = BlockPredicate<Int> {
       $0 % 2 != 0
    }
    
    let erasedOdd = odd.erase()
    let isOdd = erasedOdd.evaluate(with: 3)
    

    Declaration

    Swift

    public func erase() -> AnyPredicate<InputType>

    Return Value

    An AnyPredicate wrapping this predicate.

  • Asynchronous evaluates whether a given input matches the conditions specified by the receiver, then calls a handler upon completion.

    Declaration

    Swift

    public func evaluate(with input: InputType, queue: DispatchQueue = .main, completionHandler: @escaping (_ matches: Bool) -> Void)

    Parameters

    input

    The input against which to evaluate the receiver.

    queue

    The queue on which the completion handler is executed. If not specified, it uses DispatchQueue.main.

    completionHandler

    The completion handler to call when the evaluation is complete. It takes a Bool parameter:

    matches

    true if input matches the conditions specified by the receiver, false otherwise.