Comparable
protocol Comparable : Equatable
-
Returns
trueif the left value is ordered before or the same as the right value.Recommended over
<=
Declaration
Swift
public static func ≤ (lhs: Self, rhs: Self) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Returns
trueif the left value is ordered after or the same as the right value.Recommended over
>=
Declaration
Swift
public static func ≥ (lhs: Self, rhs: Self) -> BoolParameters
lhsA value to compare.
rhsAnother value to compare.
-
Increases the value of
selfso that falls at or aboveminimum.This is accomplished by changing
selfto match the value ofminimumif and only ifselfdoes not already satisfyself ≥ 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
minimumThe desired minimum for the value.
-
Decreases the value of
selfso that falls at or belowmaximum.This is accomplished by changing
selfto match the value ofmaximumif and only ifselfdoes not already satisfyself ≤ 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
maximumThe desired maximum for the value.
-
Returns
trueiflhsis within the range described byrhs.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)) -> BoolParameters
lhsThe value to test.
rhsThe bounds of the range.
-
Returns
trueif the left value is less than the right.Declaration
Swift
public static func < (lhs: Self, rhs: Self) -> BoolParameters
lhsA value.
rhsAnother value.
View on GitHub
Comparable Extension Reference