Accesses the element at the specified position.
SDK
- Xcode 6.3+
Framework
- Swift Standard Library
Declaration
subscript(index: Int) -> Element { get set }
Parameters
indexThe position of the element to access.
indexmust be greater than or equal tostartand less thanIndex end.Index
Discussion
The following example uses indexed subscripting to update an array’s second element. After assigning the new value ("Butler") at a specific position, that value is immediately available at that same position.
var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
streets[1] = "Butler"
print(streets[1])
// Prints "Butler"
Complexity: Reading an element from an array is O(1). Writing is O(1) unless the array’s storage is shared with another array or uses a bridged NSArray instance as its storage, in which case writing is O(n), where n is the length of the array.