Instance Methodswift 6.0.1Swift
index(_:offsetBy:limitedBy:)
Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
func index(_ i: Int, offsetBy distance: Int, limitedBy limit: Int) -> Int?
Parameters
Returns
An index offset by distance
from the index i
, unless that index would be beyond limit
in the direction of movement. In that case, the method returns nil
.
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]
if let i = numbers.index(numbers.startIndex,
offsetBy: 4,
limitedBy: numbers.endIndex) {
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.
Other members in extension
Typealiases
typealias Index
The index type for arrays,
Int
.typealias Indices
The type that represents the indices that are valid for subscripting an array, in ascending order.
typealias Iterator
The type that allows iteration over an array’s elements.
Type members
init(
) Creates a new, empty array.
init<S>(S
) Creates an array containing the elements of a sequence.
init(arrayLiteral: Element...
) Creates an array from the given array literal.
init(repeating: Element, count: Int
) Creates a new array containing the specified number of a single, repeated value.
Instance members
var capacity: Int
The total number of elements that the array can contain without allocating new storage.
var count: Int
The number of elements in the array.
var customMirror: Mirror
A mirror that reflects the array.
var debugDescription: String
A textual representation of the array and its elements, suitable for debugging.
var description: String
A textual representation of the array and its elements.
var endIndex: Int
The array’s “past the end” position—that is, the position one greater than the last valid subscript argument.
var startIndex: Int
The position of the first element in a nonempty array.
subscript(Range<Int>
) -> ArraySlice<Element> Accesses a contiguous subrange of the array’s elements.
subscript(Int
) -> Element Accesses the element at the specified position.
func append(Element
) Adds a new element at the end of the array.
func append<S>(contentsOf: S
) Adds the elements of a sequence to the end of the array.
func distance(from: Int, to: Int
) -> Int Returns the distance between two indices.
func formIndex(after: inout Int
) func formIndex(before: inout Int
) func index(Int, offsetBy: Int
) -> Int Returns an index that is the specified distance from the given index.
func index(after: Int
) -> Int Returns the position immediately after the given index.
func index(before: Int
) -> Int Returns the position immediately before the given index.
func insert(Element, at: Int
) Inserts a new element at the specified position.
func remove(at: Int
) -> Element Removes and returns the element at the specified position.
func removeAll(keepingCapacity: Bool
) Removes all elements from the array.
func replaceSubrange<C>(Range<Int>, with: C
) Replaces a range of elements with the elements in the specified collection.
func reserveCapacity(Int
) Reserves enough space to store the specified number of elements.
func withContiguousMutableStorageIfAvailable<R>((inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R? func withContiguousStorageIfAvailable<R>((UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R? func withUnsafeBufferPointer<R>((UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R Calls a closure with a pointer to the array’s contiguous storage.
func withUnsafeBytes<R>((UnsafeRawBufferPointer) throws -> R
) rethrows -> R Calls the given closure with a pointer to the underlying bytes of the array’s contiguous storage.
func withUnsafeMutableBufferPointer<R>((inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R Calls the given closure with a pointer to the array’s mutable contiguous storage.
func withUnsafeMutableBytes<R>((UnsafeMutableRawBufferPointer) throws -> R
) rethrows -> R Calls the given closure with a pointer to the underlying bytes of the array’s mutable contiguous storage.