Repeated

struct Repeated

A collection whose elements are all identical.

Inheritance RandomAccessCollection
Associated Types
public typealias Indices = Range<Int>
public typealias Index = Int

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

You create an instance of the Repeated collection by calling the repeatElement(_:count:) function. The following example creates a collection containing the name "Humperdinck" repeated five times:

let repeatedName = repeatElement("Humperdinck", count: 5)
for name in repeatedName {
    print(name)
}
// "Humperdinck"
// "Humperdinck"
// "Humperdinck"
// "Humperdinck"
// "Humperdinck"

Instance Variables

let count Required

The number of elements in this collection.

Declaration

let count: Int
var endIndex Required

The collection's "past the end" position---that is, the position one greater than the last valid subscript argument.

In a Repeated collection, endIndex is always equal to count. If the collection is empty, endIndex is equal to startIndex.

Declaration

var endIndex: Repeated<Element>.Index
let repeatedValue Required

The value of every element in this collection.

Declaration

let repeatedValue: Element
var startIndex Required

The position of the first element in a nonempty collection.

In a Repeated collection, startIndex is always equal to zero. If the collection is empty, startIndex is equal to endIndex.

Declaration

var startIndex: Repeated<Element>.Index

Subscripts

subscript subscript(position:) Required

Accesses the element at the specified position.

  • Parameter position: The position of the element to access. position must be a valid index of the collection that is not equal to the endIndex property.

Declaration

@inlinable public subscript(position: Int) -> Element

Instance Methods

func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index? Required

Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.

The following example obtains an index advanced four positions from an array's starting index and then prints the element at that position. The operation doesn't require going beyond the limiting numbers.endIndex value, so it succeeds.

let numbers = [10, 20, 30, 40, 50]
let i = numbers.index(numbers.startIndex, offsetBy: 4)
print(numbers[i])
// Prints "50"

The next example attempts to retrieve an index ten positions from numbers.startIndex, but fails, because that distance is beyond the index passed as limit.

let j = numbers.index(numbers.startIndex,
                      offsetBy: 10,
                      limitedBy: numbers.endIndex)
print(j)
// Prints "nil"

The value passed as distance must not offset i beyond the bounds of the collection, unless the index passed as limit prevents offsetting beyond those bounds.

Complexity: O(1)

Declaration

@inlinable public func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index?