Returns the position immediately after the given index.
The successor of an index must be well defined. For an index `i` into a
collection `c`, calling `c.index(after: i)` returns the same index every
time.
- Parameter i: A valid index of the collection. `i` must be less than
`endIndex`.
- Returns: The index value immediately after `i`.
Returns the position immediately before the given index.
- Parameter i: A valid index of the collection. `i` must be greater than
`startIndex`.
- Returns: The index value immediately before `i`.
get · iOS 18.0+
indices
TypeScript
get indices(): Promise<Entity_ComponentSet_Indices>
Swift declaration
var indices: Entity.ComponentSet.Indices { get }
The indices that are valid for subscripting the collection, in ascending
order.
A collection's `indices` property can hold a strong reference to the
collection itself, causing the collection to be nonuniquely referenced.
If you mutate the collection while iterating over its indices, a strong
reference can result in an unexpected copy of the collection. To avoid
the unexpected copy, use the `index(after:)` method starting with
`startIndex` to produce indices instead.
var c = MyFancyCollection([10, 20, 30, 40, 50])
var i = c.startIndex
while i != c.endIndex {
c[i] /= 5
i = c.index(after: i)
}
// c == MyFancyCollection([2, 4, 6, 8, 10])
func distance(from start: Entity.ComponentSet.Indices.Index, to end: Entity.ComponentSet.Indices.Index) -> Int
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(*k*), where *k* is the
resulting distance.
get · iOS 18.0+
endIndex
TypeScript
get endIndex(): Promise<Entity_ComponentSet_Index>
Swift declaration
var endIndex: Entity.ComponentSet.Indices.Index { get }
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.firstIndex(of: 30) {
print(numbers[index ..< numbers.endIndex])
}
// Prints "[30, 40, 50]"
If the collection is empty, `endIndex` is equal to `startIndex`.
subscript(position: Entity.ComponentSet.Indices.Index) -> Entity.ComponentSet.Indices.Index { get }
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.
- 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.
- Complexity: O(1)
subscript(bounds: Range<Entity.ComponentSet.Indices.Index>) -> Entity.ComponentSet.Indices { get }
Accesses a contiguous subrange of the collection's elements.
For example, using a `PartialRangeFrom` range expression with an array
accesses the subrange from the start of the range expression until the
end of the array.
let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2..<5]
print(streetsSlice)
// ["Channing", "Douglas", "Evarts"]
The accessed slice uses the same indices for the same elements as the
original collection. This example searches `streetsSlice` for one of the
strings in the slice, and then uses that index in the original array.
let index = streetsSlice.firstIndex(of: "Evarts")! // 4
print(streets[index])
// "Evarts"
Always use the slice's `startIndex` property instead of assuming that its
indices start at a particular value. Attempting to access an element by
using an index outside the bounds of the slice may result in a runtime
error, even if that index is valid for the original collection.
print(streetsSlice.startIndex)
// 2
print(streetsSlice[2])
// "Channing"
print(streetsSlice[0])
// error: Index out of bounds
- Parameter bounds: A range of the collection's indices. The bounds of
the range must be valid indices of the collection.
- Complexity: O(1)