RangeReplaceableIndexable

protocol RangeReplaceableIndexable

A type that supports replacement of an arbitrary subrange of elements with the elements of another collection.

In most cases, it's best to ignore this protocol and use the RangeReplaceableCollection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'RandomAccessCollection' instead.

Inheritance Indexable, IndexableBase View Protocol Hierarchy →
Associated Types
IndexDistance : SignedInteger = Int

A type used to represent the number of steps between two indices, where one value is reachable from the other.

In Swift, reachability refers to the ability to produce one value from the other through zero or more applications of index(after:).

Index : Comparable

A type that represents a 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 argument.

See Also: endIndex

SubSequence

A sequence that can represent a contiguous subrange of the collection's elements.

Import import Swift

Initializers

init() Required

Creates an empty instance.

Declaration

init()
init(_:) Required

Creates a new instance of a collection containing the elements of a sequence.

elements: The sequence of elements for the new collection. elements must be finite.

Declaration

init<S : Sequence where S.Iterator.Element == _Element>(_ elements: S)
init(repeating:count:) Required

Creates a new collection containing the specified number of a single, repeated value.

Here's an example of creating an array initialized with five strings containing the letter Z.

let fiveZs = Array(repeating: "Z", count: 5)
print(fiveZs)
// Prints "["Z", "Z", "Z", "Z", "Z"]"

Parameters: repeatedValue: The element to repeat. count: The number of times to repeat the value passed in the repeating parameter. count must be zero or greater.

Declaration

init(repeating repeatedValue: Self._Element, count: Int)

Instance Variables

var endIndex: Self.Index Required

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

When you need a range that includes the last element of a collection, use the half-open range operator (..<) with endIndex. The ..< operator creates a range that doesn't include the upper bound, so it's always safe to use with endIndex. For example:

let numbers = [10, 20, 30, 40, 50]
if let index = numbers.index(of: 30) {
    print(numbers[index ..< numbers.endIndex])
}
// Prints "[30, 40, 50]"

If the collection is empty, endIndex is equal to startIndex.

Declaration

var endIndex: Self.Index { get }

Declared In

IndexableBase
var startIndex: Self.Index Required

The position of the first element in a nonempty collection.

If the collection is empty, startIndex is equal to endIndex.

Declaration

var startIndex: Self.Index { get }

Declared In

IndexableBase

Subscripts

subscript(_: Self.Index) Required

Accesses the element at the specified position.

The following example accesses an element of an array through its subscript to print its value:

var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
print(streets[1])
// Prints "Bryant"

You can subscript a collection with any valid index other than the collection's end index. The end index refers to the position one past the last element of a collection, so it doesn't correspond with an element.

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

subscript(position: Self.Index) -> Self._Element { get }

Declared In

IndexableBase
subscript(_: Range<Self.Index>) Required

Accesses the subsequence bounded by the given range.

bounds: A range of the collection's indices. The upper and lower bounds of the bounds range must be valid indices of the collection.

Declaration

subscript(bounds: Range<Self.Index>) -> Self.SubSequence { get }

Declared In

IndexableBase

Instance Methods

func distance(from:to:)

Returns the distance between two indices.

Unless the collection conforms to the BidirectionalCollection protocol, start must be less than or equal to end.

Parameters: start: A valid index of the collection. end: Another valid index of the collection. If end is equal to start, the result is zero. Returns: The distance between start and end. The result can be negative only if the collection conforms to the BidirectionalCollection protocol.

Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the resulting distance.

Declaration

func distance(from start: Self.Index, to end: Self.Index) -> Self.IndexDistance

Declared In

Indexable
func formIndex(_:offsetBy:)

Offsets the given index by the specified distance.

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol.

See Also: index(_:offsetBy:), formIndex(_:offsetBy:limitedBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func formIndex(_ i: inout Self.Index, offsetBy n: Self.IndexDistance)

Declared In

Indexable
func formIndex(_:offsetBy:limitedBy:)

Offsets the given index by the specified distance, or so that it equals the given limiting index.

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection, unless the index passed as limit prevents offsetting beyond those bounds.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol. Returns: true if i has been offset by exactly n steps without going beyond limit; otherwise, false. When the return value is false, the value of i is equal to limit.

See Also: index(_:offsetBy:), formIndex(_:offsetBy:limitedBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func formIndex(_ i: inout Self.Index, offsetBy n: Self.IndexDistance, limitedBy limit: Self.Index) -> Bool

Declared In

Indexable
func formIndex(after:)

Replaces the given index with its successor.

i: A valid index of the collection. i must be less than endIndex.

Declaration

func formIndex(after i: inout Self.Index)

Declared In

IndexableBase
func index(_:offsetBy:)

Returns an index that is the specified distance from the given index.

The following example obtains an index advanced four positions from a string's starting index and then prints the character at that position.

let s = "Swift"
let i = s.index(s.startIndex, offsetBy: 4)
print(s[i])
// Prints "t"

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol. Returns: An index offset by n from the index i. If n is positive, this is the same value as the result of n calls to index(after:). If n is negative, this is the same value as the result of -n calls to index(before:).

See Also: index(_:offsetBy:limitedBy:), formIndex(_:offsetBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func index(_ i: Self.Index, offsetBy n: Self.IndexDistance) -> Self.Index

Declared In

Indexable
func index(_:offsetBy:limitedBy:)

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 a string's starting index and then prints the character at that position. The operation doesn't require going beyond the limiting s.endIndex value, so it succeeds.

let s = "Swift"
if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
    print(s[i])
}
// Prints "t"

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

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

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection, unless the index passed as limit prevents offsetting beyond those bounds.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol. limit: A valid index of the collection to use as a limit. If n > 0, a limit that is less than i has no effect. Likewise, if n < 0, a limit that is greater than i has no effect. Returns: An index offset by n from the index i, unless that index would be beyond limit in the direction of movement. In that case, the method returns nil.

See Also: index(_:offsetBy:), formIndex(_:offsetBy:limitedBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func index(_ i: Self.Index, offsetBy n: Self.IndexDistance, limitedBy limit: Self.Index) -> Self.Index?

Declared In

Indexable
func index(after:) Required

Returns the position immediately after the given index.

i: A valid index of the collection. i must be less than endIndex. Returns: The index value immediately after i.

Declaration

func index(after i: Self.Index) -> Self.Index

Declared In

IndexableBase
mutating func insert(_:at:) Required

Inserts a new element into the collection at the specified position.

The new element is inserted before the element currently at the specified index. If you pass the collection's endIndex property as the i parameter, the new element is appended to the collection.

var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)

print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"

Calling this method may invalidate any existing indices for use with this collection.

Parameters: newElement: The new element to insert into the collection. i: The position at which to insert the new element. i must be a valid index into the collection.

Complexity: O(n), where n is the length of the collection.

Declaration

mutating func insert(_ newElement: Self._Element, at i: Self.Index)
mutating func insert(contentsOf:at:) Required

Inserts the elements of a sequence into the collection at the specified position.

The new elements are inserted before the element currently at the specified index. If you pass the collection's endIndex property as the i parameter, the new elements are appended to the collection.

Here's an example of inserting a range of integers into an array of the same type:

var numbers = [1, 2, 3, 4, 5]
numbers.insert(contentsOf: 100...103, at: 3)
print(numbers)
// Prints "[1, 2, 3, 100, 101, 102, 103, 4, 5]"

Calling this method may invalidate any existing indices for use with this collection.

Parameters: newElements: The new elements to insert into the collection. i: The position at which to insert the new elements. i must be a valid index of the collection.

Complexity: O(m), where m is the combined length of the collection and newElements. If i is equal to the collection's endIndex property, the complexity is O(n), where n is the length of newElements.

Declaration

mutating func insert<S : Collection where S.Iterator.Element == _Element>(contentsOf newElements: S, at i: Self.Index)
mutating func remove(at:) Required

Removes and returns the element at the specified position.

All the elements following the specified position are moved to close the gap. This example removes the middle element from an array of measurements.

var measurements = [1.2, 1.5, 2.9, 1.2, 1.6]
let removed = measurements.remove(at: 2)
print(measurements)
// Prints "[1.2, 1.5, 1.2, 1.6]"

Calling this method may invalidate any existing indices for use with this collection.

i: The position of the element to remove. i must be a valid index of the collection that is not equal to the collection's end index. Returns: The removed element.

Complexity: O(n), where n is the length of the collection.

Declaration

mutating func remove(at i: Self.Index) -> Self._Element
mutating func removeSubrange(_:) Required

Removes the specified subrange of elements from the collection.

var bugs = ["Aphid", "Bumblebee", "Cicada", "Damselfly", "Earwig"]
bugs.removeSubrange(1...3)
print(bugs)
// Prints "["Aphid", "Earwig"]"

Calling this method may invalidate any existing indices for use with this collection.

bounds: The subrange of the collection to remove. The bounds of the range must be valid indices of the collection.

Complexity: O(n), where n is the length of the collection.

Declaration

mutating func removeSubrange(_ bounds: Range<Self.Index>)
mutating func replaceSubrange(_:with:) Required

Replaces the specified subrange of elements with the given collection.

This method has the effect of removing the specified range of elements from the collection and inserting the new elements at the same location. The number of new elements need not match the number of elements being removed.

In this example, three elements in the middle of an array of integers are replaced by the five elements of a Repeated<Int> instance.

 var nums = [10, 20, 30, 40, 50]
 nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
 print(nums)
 // Prints "[10, 1, 1, 1, 1, 1, 50]"

If you pass a zero-length range as the subrange parameter, this method inserts the elements of newElements at subrange.startIndex. Calling the insert(contentsOf:at:) method instead is preferred.

Likewise, if you pass a zero-length collection as the newElements parameter, this method removes the elements in the given subrange without replacement. Calling the removeSubrange(_:) method instead is preferred.

Calling this method may invalidate any existing indices for use with this collection.

Parameters: subrange: The subrange of the collection to replace. The bounds of the range must be valid indices of the collection. newElements: The new elements to add to the collection.

Complexity: O(m), where m is the combined length of the collection and newElements. If the call to replaceSubrange simply appends the contents of newElements to the collection, the complexity is O(n), where n is the length of newElements.

Declaration

mutating func replaceSubrange<C where C : Collection, C.Iterator.Element == _Element>(_ subrange: Range<Self.Index>, with newElements: C)

Default Implementations

subscript(_: ClosedRange<Self.Index>)

Accesses a contiguous subrange of the collection's elements.

The accessed slice uses the same indices for the same elements as the original collection. Always use the slice's startIndex property instead of assuming that its indices start at a particular value.

This example demonstrates getting a slice of an array of strings, finding the index of one of the strings in the slice, and then using that index in the original array.

let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2 ..< streets.endIndex]
print(streetsSlice)
// Prints "["Channing", "Douglas", "Evarts"]"

let index = streetsSlice.index(of: "Evarts")    // 4
print(streets[index!])
// Prints "Evarts"

bounds: A range of the collection's indices. The bounds of the range must be valid indices of the collection.

Declaration

subscript(bounds: ClosedRange<Self.Index>) -> Self.SubSequence { get }

Declared In

Indexable
func distance(from:to:)

Returns the distance between two indices.

Unless the collection conforms to the BidirectionalCollection protocol, start must be less than or equal to end.

Parameters: start: A valid index of the collection. end: Another valid index of the collection. If end is equal to start, the result is zero. Returns: The distance between start and end. The result can be negative only if the collection conforms to the BidirectionalCollection protocol.

Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the resulting distance.

Declaration

func distance(from start: Self.Index, to end: Self.Index) -> Self.IndexDistance

Declared In

Indexable
func formIndex(_:offsetBy:)

Offsets the given index by the specified distance.

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol.

See Also: index(_:offsetBy:), formIndex(_:offsetBy:limitedBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func formIndex(_ i: inout Self.Index, offsetBy n: Self.IndexDistance)

Declared In

Indexable
func formIndex(_:offsetBy:limitedBy:)

Offsets the given index by the specified distance, or so that it equals the given limiting index.

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection, unless the index passed as limit prevents offsetting beyond those bounds.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol. Returns: true if i has been offset by exactly n steps without going beyond limit; otherwise, false. When the return value is false, the value of i is equal to limit.

See Also: index(_:offsetBy:), formIndex(_:offsetBy:limitedBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func formIndex(_ i: inout Self.Index, offsetBy n: Self.IndexDistance, limitedBy limit: Self.Index) -> Bool

Declared In

Indexable
func formIndex(after:)

Replaces the given index with its successor.

i: A valid index of the collection. i must be less than endIndex.

Declaration

func formIndex(after i: inout Self.Index)

Declared In

Indexable
func index(_:offsetBy:)

Returns an index that is the specified distance from the given index.

The following example obtains an index advanced four positions from a string's starting index and then prints the character at that position.

let s = "Swift"
let i = s.index(s.startIndex, offsetBy: 4)
print(s[i])
// Prints "t"

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol. Returns: An index offset by n from the index i. If n is positive, this is the same value as the result of n calls to index(after:). If n is negative, this is the same value as the result of -n calls to index(before:).

See Also: index(_:offsetBy:limitedBy:), formIndex(_:offsetBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func index(_ i: Self.Index, offsetBy n: Self.IndexDistance) -> Self.Index

Declared In

Indexable
func index(_:offsetBy:limitedBy:)

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 a string's starting index and then prints the character at that position. The operation doesn't require going beyond the limiting s.endIndex value, so it succeeds.

let s = "Swift"
if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
    print(s[i])
}
// Prints "t"

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

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

The value passed as n must not offset i beyond the endIndex or before the startIndex of this collection, unless the index passed as limit prevents offsetting beyond those bounds.

Parameters: i: A valid index of the collection. n: The distance to offset i. n must not be negative unless the collection conforms to the BidirectionalCollection protocol. limit: A valid index of the collection to use as a limit. If n > 0, a limit that is less than i has no effect. Likewise, if n < 0, a limit that is greater than i has no effect. Returns: An index offset by n from the index i, unless that index would be beyond limit in the direction of movement. In that case, the method returns nil.

See Also: index(_:offsetBy:), formIndex(_:offsetBy:limitedBy:) Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the absolute value of n.

Declaration

func index(_ i: Self.Index, offsetBy n: Self.IndexDistance, limitedBy limit: Self.Index) -> Self.Index?

Declared In

Indexable

Where Index : Strideable, Index.Stride : SignedInteger

subscript(_: CountableClosedRange<Self.Index>)

Accesses a contiguous subrange of the collection's elements.

The accessed slice uses the same indices for the same elements as the original collection. Always use the slice's startIndex property instead of assuming that its indices start at a particular value.

This example demonstrates getting a slice of an array of strings, finding the index of one of the strings in the slice, and then using that index in the original array.

let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2 ..< streets.endIndex]
print(streetsSlice)
// Prints "["Channing", "Douglas", "Evarts"]"

let index = streetsSlice.index(of: "Evarts")    // 4
print(streets[index!])
// Prints "Evarts"

bounds: A range of the collection's indices. The bounds of the range must be valid indices of the collection.

Declaration

subscript(bounds: CountableClosedRange<Self.Index>) -> Self.SubSequence { get }

Declared In

Indexable
subscript(_: CountableRange<Self.Index>)

Accesses a contiguous subrange of the collection's elements.

The accessed slice uses the same indices for the same elements as the original collection. Always use the slice's startIndex property instead of assuming that its indices start at a particular value.

This example demonstrates getting a slice of an array of strings, finding the index of one of the strings in the slice, and then using that index in the original array.

let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2 ..< streets.endIndex]
print(streetsSlice)
// Prints "["Channing", "Douglas", "Evarts"]"

let index = streetsSlice.index(of: "Evarts")    // 4
print(streets[index!])
// Prints "Evarts"

bounds: A range of the collection's indices. The bounds of the range must be valid indices of the collection.

Declaration

subscript(bounds: CountableRange<Self.Index>) -> Self.SubSequence { get }

Declared In

Indexable