Range

struct Range<T : ForwardIndexType>

A collection of consecutive discrete index values.

T is both the element type and the index type of the collection.

Like other collections, a range containing one element has an endIndex that is the successor of its startIndex; and an empty range has startIndex == endIndex.

Axiom: for any Range r, r[i] == i.

Therefore, if T has a maximal value, it can serve as an endIndex, but can never be contained in a Range<T>.

It also follows from the axiom above that (-99..<100)[0] == 0. To prevent confusion (because some expect the result to be -99), in a context where T is known to be an integer type, subscripting with T is a compile-time error:

// error: could not find an overload for 'subscript'...
println( Range<Int>(start:-99, end:100)[0] )

However, subscripting that range still works in a generic context:

func brackets<T:ForwardIndexType>(x: Range<T>, i: T) -> T {
  return x[i] // Just forward to subscript
}
println(brackets(Range<Int>(start:-99, end:100), 0)) // prints 0
Inheritance CollectionType, DebugPrintable, Equatable, Printable, Reflectable, SequenceType, _CollectionType, _SequenceType, _Sequence_Type View Protocol Hierarchy →
Associated Types
Index = T

A type that represents a valid position in the collection.

Valid indices consist of the position of every element and a "past the end" position that's not valid for use as a subscript.

ArraySlice = Range<T>
Generator = RangeGenerator<T>

A type whose instances can produce the elements of this sequence, in order.

Element = T

Type alias inferred.

Index = T

Type alias inferred.

SubSequence = Slice<Range<T>>

Type alias inferred.

Import import Swift

Initializers

init(_:)

Construct a copy of x

Declaration

init(_ x: Range<T>)
init(start:end:)

Construct a range with startIndex == start and endIndex == end.

Declaration

init(start: T, end: T)

Instance Variables

var debugDescription: String

A textual representation of self, suitable for debugging.

Declaration

var debugDescription: String { get }
var description: String

A textual representation of self.

Declaration

var description: String { get }
var endIndex: T

The range's upper bound

endIndex is not a valid argument to subscript, and is always reachable from startIndex by zero or more applications of successor().

Declaration

var endIndex: T { get set }
var isEmpty: Bool

true iff the range is empty, i.e. startIndex == endIndex

Declaration

var isEmpty: Bool { get }
var startIndex: T

The range's lower bound

Identical to endIndex in an empty range.

Declaration

var startIndex: T { get set }

Subscripts

subscript(_: T)

Declaration

subscript(position: T) -> T { get }
subscript(_: T._DisabledRangeIndex)

Declaration

subscript(_: T._DisabledRangeIndex) -> T { get }

Instance Methods

func generate()

Return a generator over the elements of this sequence.

Complexity: O(1)

Declaration

func generate() -> RangeGenerator<T>
func getMirror()

Returns a mirror that reflects self.

Declaration

func getMirror() -> MirrorType
func map(_:)

Return an array containing the results of calling transform(x) on each element x of self.

Declaration

func map<U>(transform: (T) -> U) -> [U]