Comparable

protocol Comparable : Equatable
  • Returns true if the left value is ordered before or the same as the right value.

    Declaration

    Swift

    public static func  (lhs: Self, rhs: Self) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns true if the left value is ordered after or the same as the right value.

    Declaration

    Swift

    public static func  (lhs: Self, rhs: Self) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Increases the value of self so that falls at or above minimum.

    This is accomplished by changing self to match the value of minimum if and only if self does not already satisfy self ≥ minimum.

    For example:

    let numberOfRolls = 5
    var highestRoll = 1
    for _ in 1 ... numberOfRolls {
        highestRoll.increase(to: rollDie())
    }
    print("After rolling the die \(numberOfRolls) time(s), the highest roll was \(highestRoll).")
    // Prints, for example, “After rolling the die 5 time(s), the highest roll was 4.”
    
    // In this example, rollDie() represents a function that randomly returns an Int between 1 and 6 inclusive. In each iteration of the for loop, a new random number is generated, and if it is greater than highestRoll’s existing value, increase(to:) changes highestRoll to reflect the new high.
    

    Nonmutating variant

    max

    Declaration

    Swift

    public mutating func increase(to minimum: Self)

    Parameters

    minimum

    The desired minimum for the value.

  • Decreases the value of self so that falls at or below maximum.

    This is accomplished by changing self to match the value of maximum if and only if self does not already satisfy self ≤ maximum.

    For example:

    let numberOfRolls = 5
    var lowestRoll = 6
    for _ in 1 ... numberOfRolls {
        lowestRoll.decrease(to: rollDie())
    }
    print("After rolling the die \(numberOfRolls) time(s), the lowest roll was \(lowestRoll).")
    // Prints, for example, “After rolling the die 5 time(s), the lowest roll was 2.”
    
    // In this example, rollDie() represents a function that randomly returns an Int between 1 and 6 inclusive. In each iteration of the for loop, a new random number is generated, and if it is less than lowestRoll’s existing value, decrease(to:) changes lowestRoll to reflect the new low.
    

    Nonmutating variant

    min

    Declaration

    Swift

    public mutating func decrease(to maximum: Self)

    Parameters

    maximum

    The desired maximum for the value.

  • Returns true if lhs is within the range described by rhs.

    if 1 ÷ 3  0.33333 ± 0.00001 {
        print("It is accurate to at least four digits.")
    }
    

    Declaration

    Swift

    public static func  (lhs: Self, rhs: (Self, Self)) -> Bool

    Parameters

    lhs

    The value to test.

    rhs

    The bounds of the range.

  • Returns true if the left value is less than the right.

    Declaration

    Swift

    public static func < (lhs: Self, rhs: Self) -> Bool

    Parameters

    lhs

    A value.

    rhs

    Another value.