Predicate

public protocol Predicate<InputType> : 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.

Dynamic Lookup Extension

  • block(evaluationBlock:) Extension method

    Returns a new BlockPredicate instance.

    let predicate: BlockPredicate<Int> = .block {
       $0 % 2 == 0
    }
    
    let isEven = even.evaluate(with: 2)
    

    Declaration

    Swift

    public static func block<T>(evaluationBlock: @escaping (_ input: T) -> Bool) -> Self where Self == BlockPredicate<T>

    Parameters

    evaluationBlock

    A closure describing a custom validation condition.

    input

    The input against which to evaluate the receiver.

  • length(min:max:) Extension method

    Returns a new LengthPredicate instance.

    let predicate: LengthPredicate<String> = .length(min: 8, max: 64)
    let isValid = predicate.evaluate(with: "p@ssW0rd")
    

    Declaration

    Swift

    public static func length<T>(min: Int? = nil, max: Int? = nil) -> Self where Self == LengthPredicate<T>, T : Collection

    Parameters

    min

    The lower bound of the range.

    max

    The upper bound of the range.

  • length(_:) Extension method

    Creates and returns a new LengthPredicate instance.

    let predicate: LengthPredicate<String> = .length(8...64)
    let isValid = predicate.evaluate(with: "p@ssW0rd")
    

    Declaration

    Swift

    public static func length<T>(_ range: ClosedRange<Int>) -> Self where Self == LengthPredicate<T>, T : Collection

    Parameters

    range

    A ClosedRange that defines the lower and upper bounds of the range.

  • length(_:) Extension method

    Creates and returns a new RangePredicate instance.

    let predicate: LengthPredicate<String> = .length(8..<65)
    let isValid = predicate.evaluate(with: "p@ssW0rd")
    

    Declaration

    Swift

    public static func length<T>(_ range: Range<Int>) -> Self where Self == LengthPredicate<T>, T : Collection

    Parameters

    range

    A Range that defines the lower and upper bounds of the range.

  • range(min:max:) Extension method

    Returns a new RangePredicate instance.

    let drinkingAgeLimit: RangePredicate = .range(min: 21)
    let isAllowed = drinkingAgeLimit.evaluate(with: 18)
    

    Declaration

    Swift

    public static func range<T>(min: T? = nil, max: T? = nil) -> Self where Self == RangePredicate<T>, T : Comparable

    Parameters

    min

    The lower bound of the range.

    max

    The upper bound of the range.

  • range(_:) Extension method

    Returns a new RangePredicate instance.

    let predicate: RangePredicate = .range(21...90)
    let isAllowed = predicate.evaluate(with: 25)
    

    Declaration

    Swift

    public static func range<T>(_ range: ClosedRange<T>) -> Self where Self == RangePredicate<T>, T : Comparable

    Parameters

    range

    A ClosedRange that defines the lower and upper bounds of the range.

  • range(_:) Extension method

    Returns a new RangePredicate instance.

    let predicate: RangePredicate = .range(21..<91)
    let isAllowed = predicate.evaluate(with: 25)
    

    Declaration

    Swift

    public static func range<T>(_ range: Range<T>) -> Self where Self == RangePredicate<T>, T : Strideable, T.Stride : SignedInteger

    Parameters

    range

    A Range that defines the lower and upper bounds of the range.

  • required() Extension method

    Returns a new RequiredPredicate instance.

    let predicate: RequiredPredicate<String> = .required
    let isValid = predicate.evaluate(with: "")
    

    Declaration

    Swift

    public static func required<T>() -> Self where Self == RequiredPredicate<T>, T : Collection

Available where Self == CharacterSetPredicate

  • characterSet(_:mode:) Extension method

    Returns a new CharacterSetPredicate instance.

    let predicate: CharacterSetPredicate = .characterSet(.lowercaseLetters)
    let hasOnlyLowercaseLetters = predicate.evaluate(with: "letters")
    

    Declaration

    Swift

    public static func characterSet(_ characterSet: CharacterSet, mode: CharacterSetPredicate.Mode = .strict) -> Self

    Parameters

    characterSet

    A CharacterSet used to evaluate a given String input.

    mode

    A Mode that describes how the input’s character set should be evaluated against the provider’s character set.

Available where Self == EmailPredicate

  • email Extension method

    Returns a new EmailPredicate instance.

    let predicate: EmailPredicate = .email
    let isEmail = predicate.evaluate(with: "hello@nsagora.com")
    

    Declaration

    Swift

    public static var email: Self { get }

Available where Self == RegexPredicate

  • regex(_:) Extension method

    Returns a new RegexPredicate instance.

    let predicate: RegexPredicate = .regex("^\\d+$")
    let isValid = predicate.evaluate(with: "1234567890")
    

    Declaration

    Swift

    public static func regex(_ expression: String) -> Self

    Parameters

    expression

    A String describing the regular expression.

Available where Self == URLPredicate

  • url Extension method

    Returns a new URLPredicate instance.

    let predicate: URLPredicate = .url
    let isValid = predicate.evaluate(with: "http://www.swift.org")
    

    Declaration

    Swift

    public static var url: Self { get }