struct
Range
<
Bound
:
Comparable
>
Inheritance |
CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Equatable
View Protocol Hierarchy →
|
---|---|
Import |
|
Initializers
Creates an instance equivalent to the given range.
An equivalent range must be representable as an instance of Range
.
For example, passing a closed range with an upper bound of Int.max
triggers a runtime error, because the resulting half-open range would
require an upper bound of Int.max + 1
, which is not representable as
an Int
.
other
: A range to convert to a Range
instance.
Declaration
init
(
_
other
:
ClosedRange
<
Bound
>
)
Creates an instance equivalent to the given range.
An equivalent range must be representable as an instance of Range
.
For example, passing a closed range with an upper bound of Int.max
triggers a runtime error, because the resulting half-open range would
require an upper bound of Int.max + 1
, which is not representable as
an Int
.
other
: A range to convert to a Range
instance.
Declaration
init
(
_
other
:
CountableClosedRange
<
Bound
>
)
Creates an instance equivalent to the given range.
other
: A range to convert to a Range
instance.
Declaration
init
(
_
other
:
CountableRange
<
Bound
>
)
Creates an instance equivalent to the given range.
other
: A range to convert to a Range
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 half-open range operator
(..<
) to form Range
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
}
The range's lower bound.
In an empty range, lowerBound
is equal to upperBound
.
Declaration
var
lowerBound
:
Bound
{
get
}
The range's upper bound.
In an empty range, upperBound
is equal to lowerBound
. A Range
instance does not contain its upper bound.
Declaration
var
upperBound
:
Bound
{
get
}
Instance Methods
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
:
Range
=
0
..
<
20
(
x
.
clamped
(
to
:
10
..
<
1000
))
// Prints "10..<20"
If the two ranges do not overlap, the result is an empty range within the
bounds of limits
.
let
y
:
Range
=
0
..
<
5
(
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
Returns a Boolean value indicating whether the given element is contained within the range.
Because Range
represents a half-open range, a Range
instance does not
contain its upper bound. element
is contained in the range if it is
greater than or equal to the lower bound and less than the upper 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
:
Range
=
0
..
<
20
(
x
.
overlaps
(
10
...
1000
as
ClosedRange
))
// Prints "true"
Because a half-open range does not include its upper bound, the ranges in the following example do not overlap:
let
y
:
ClosedRange
=
20
..
<
30
(
x
.
overlaps
(
y
))
// Prints "false"
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
:
Range
=
0
..
<
20
(
x
.
overlaps
(
10
...
1000
as
CountableClosedRange
))
// Prints "true"
Because a half-open range does not include its upper bound, the ranges in the following example do not overlap:
let
y
:
CountableClosedRange
=
20
..
<
30
(
x
.
overlaps
(
y
))
// Prints "false"
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
:
Range
=
0
..
<
20
(
x
.
overlaps
(
10
..
<
1000
as
CountableRange
))
// Prints "true"
Because a half-open range does not include its upper bound, the ranges in the following example do not overlap:
let
y
:
CountableRange
=
20
..
<
30
(
x
.
overlaps
(
y
))
// Prints "false"
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:
Because a half-open range does not include its upper bound, the ranges in the following example do not overlap:
let
y
:
Range
=
20
..
<
30
(
x
.
overlaps
(
y
))
// Prints "false"
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
.
A half-open interval over a comparable type, from a lower bound up to, but not including, an upper bound.
You create
Range
instances by using the half-open range operator (..<
).You can use a
Range
instance to quickly check if a value is contained in a particular range of values. For example:Range
instances can represent an empty interval, unlikeClosedRange
.See Also:
CountableRange
,ClosedRange
,CountableClosedRange