struct ClosedRange<Bound where Bound : Comparable>
Inheritance 
CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Equatable
View Protocol Hierarchy →


Import  import Swift 
Initializers
Creates an instance equivalent to the given range.
other
: A range to convert to a ClosedRange
instance.
Declaration
init(_ other: ClosedRange<Bound>)
Creates an instance equivalent to the given range.
other
: A range to convert to a ClosedRange
instance.
Declaration
init(_ other: CountableClosedRange<Bound>)
Creates an instance equivalent to the given range.
An equivalent range must be representable as an instance of ClosedRange
.
For example, passing an empty range as other
triggers a runtime error,
because an empty range cannot be represented by a ClosedRange
instance.
other
: A range to convert to a ClosedRange
instance.
Declaration
init(_ other: CountableRange<Bound>)
Creates an instance equivalent to the given range.
An equivalent range must be representable as an instance of ClosedRange
.
For example, passing an empty range as other
triggers a runtime error,
because an empty range cannot be represented by a ClosedRange
instance.
other
: A range to convert to a ClosedRange
instance.
Declaration
init(_ other: Range<Bound>)
Creates an instance with the given bounds.
Because this initializer does not perform any checks, it should be used
as an optimization only when you are absolutely certain that lower
is
less than or equal to upper
. Using the closed range operator (...
)
to form ClosedRange
instances is preferred.
bounds
: A tuple of the lower and upper bounds of the range.
Declaration
init(uncheckedBounds bounds: (lower: Bound, upper: Bound))
Instance Variables
The number of values contained in the range.
Declaration
var count: Bound.Stride { get }
The custom mirror for this instance.
If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.
Declaration
var customMirror: Mirror { get }
A textual representation of the range, suitable for debugging.
Declaration
var debugDescription: String { get }
A textual representation of the range.
Declaration
var description: String { get }
A Boolean value indicating whether the range contains no elements.
Because a closed range cannot represent an empty range, this property is
always false
.
Declaration
var isEmpty: Bool { get }
The range's lower bound.
Declaration
var lowerBound: Bound { get }
The range's upper bound.
Declaration
var upperBound: Bound { get }
Instance Methods
Returns a Boolean value indicating whether two ranges are equal.
Two ranges are equal when they have the same lower and upper bounds.
let x: ClosedRange = 5...15
print(x == 5...15)
// Prints "true"
print(x == 10...20)
// Prints "false"
Parameters: lhs: A range to compare. rhs: Another range to compare.
Declaration
func ==(lhs: ClosedRange<Bound>, rhs: ClosedRange<Bound>) > Bool
Returns a Boolean value indicating whether a value is included in a range.
You can use this pattern matching operator (~=
) to test whether a value
is included in a range. The following example uses the ~=
operator to
test whether an integer is included in a range of singledigit numbers.
let chosenNumber = 3
if 0...9 ~= chosenNumber {
print("\(chosenNumber) is a single digit.")
}
// Prints "3 is a single digit."
The ~=
operator is used internally in case
statements for pattern
matching. When you match against a range in a case
statement, this
operator is called behind the scenes.
switch chosenNumber {
case 0...9:
print("\(chosenNumber) is a single digit.")
case Int.min..<0:
print("\(chosenNumber) is negative.")
default:
print("\(chosenNumber) is positive.")
}
// Prints "3 is a single digit."
Parameters:
lhs: A range.
rhs: A value to match against lhs
.
Declaration
func ~=(pattern: ClosedRange<Bound>, value: Bound) > Bool
Returns a copy of this range clamped to the given limiting range.
The bounds of the result are always limited to the bounds of limits
.
For example:
let x: ClosedRange = 0...20
print(x.clamped(to: 10...1000))
// Prints "10...20"
If the two ranges do not overlap, the result is a singleelement range at
the upper or lower bound of limits
.
let y: ClosedRange = 0...5
print(y.clamped(to: 10...1000))
// Prints "10...10"
limits
: The range to clamp the bounds of this range.
Returns: A new range clamped to the bounds of limits
.
Declaration
func clamped(to limits: ClosedRange<Bound>) > ClosedRange<Bound>
Returns a Boolean value indicating whether the given element is contained within the range.
A ClosedRange
instance contains both its lower and upper bound.
element
is contained in the range if it is between the two bounds or
equal to either bound.
element
: The element to check for containment.
Returns: true
if element
is contained in the range; otherwise,
false
.
Declaration
func contains(_ element: Bound) > Bool
Returns a Boolean value indicating whether this range and the given range contain an element in common.
This example shows two overlapping ranges:
let x: ClosedRange = 0...20
print(x.overlaps(10...1000 as ClosedRange))
// Prints "true"
Because a closed range includes its upper bound, the ranges in the following example also overlap:
let y: ClosedRange = 20...30
print(x.overlaps(y))
// Prints "true"
other
: A range to check for elements in common.
Returns: true
if this range and other
have at least one element in
common; otherwise, false
.
Declaration
func overlaps(_ other: ClosedRange<Bound>) > Bool
Returns a Boolean value indicating whether this range and the given range contain an element in common.
This example shows two overlapping ranges:
let x: ClosedRange = 0...20
print(x.overlaps(10...1000 as CountableClosedRange))
// Prints "true"
Because a closed range includes its upper bound, the ranges in the following example also overlap:
let y: CountableClosedRange = 20...30
print(x.overlaps(y))
// Prints "true"
other
: A range to check for elements in common.
Returns: true
if this range and other
have at least one element in
common; otherwise, false
.
Declaration
func overlaps(_ other: CountableClosedRange<Bound>) > Bool
Returns a Boolean value indicating whether this range and the given range contain an element in common.
This example shows two overlapping ranges:
let x: ClosedRange = 0...20
print(x.overlaps(10..<1000 as CountableRange))
// Prints "true"
Because a closed range includes its upper bound, the ranges in the following example also overlap:
let y: CountableRange = 20...30
print(x.overlaps(y))
// Prints "true"
other
: A range to check for elements in common.
Returns: true
if this range and other
have at least one element in
common; otherwise, false
.
Declaration
func overlaps(_ other: CountableRange<Bound>) > Bool
Returns a Boolean value indicating whether this range and the given range contain an element in common.
This example shows two overlapping ranges:
let x: ClosedRange = 0...20
print(x.overlaps(10..<1000 as Range))
// Prints "true"
Because a closed range includes its upper bound, the ranges in the following example also overlap:
let y: Range = 20...30
print(x.overlaps(y))
// Prints "true"
other
: A range to check for elements in common.
Returns: true
if this range and other
have at least one element in
common; otherwise, false
.
Declaration
func overlaps(_ other: Range<Bound>) > Bool
An interval over a comparable type, from a lower bound up to, and including, an upper bound.
You create instances of
ClosedRange
by using the closed range operator (...
).You can use a
ClosedRange
instance to quickly check if a value is contained in a particular range of values. For example:Unlike
Range
, instances ofClosedRange
cannot represent an empty interval.See Also:
CountableRange
,Range
,CountableClosedRange