實例下標
subscript(_:)
訪問集合元素的連續子範圍。
必需的。
聲明
override subscript(bounds: Range<Self.Index>) -> Self.SubSequence { get }
參數
bounds
一係列集合的索引。範圍的邊界必須是集合的有效索引。
概述
訪問的切片對與原始集合使用的相同元素使用相同的索引。始終使用切片的 startIndex
屬性,而不是假設其索引從特定值開始。
此示例演示獲取字符串數組的切片,查找切片中字符串之一的索引,然後在原始數組中使用該索引。
let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2 ..< streets.endIndex]
print(streetsSlice)
// Prints "["Channing", "Douglas", "Evarts"]"
let index = streetsSlice.firstIndex(of: "Evarts") // 4
print(streets[index!])
// Prints "Evarts"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift RandomAccessCollection indices用法及代碼示例
- Swift RandomAccessCollection index(_:offsetBy:)用法及代碼示例
- Swift RandomAccessCollection index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift RandomNumberGenerator用法及代碼示例
- Swift RangeReplaceableCollection insert(_:at:)用法及代碼示例
- Swift Range clamped(to:)用法及代碼示例
- Swift RangeReplaceableCollection filter(_:)用法及代碼示例
- Swift RangeReplaceableCollection removeFirst(_:)用法及代碼示例
- Swift RangeReplaceableCollection用法及代碼示例
- Swift RangeReplaceableCollection removeSubrange(_:)用法及代碼示例
- Swift RangeReplaceableCollection replaceSubrange(_:with:)用法及代碼示例
- Swift RangeReplaceableCollection removeFirst()用法及代碼示例
- Swift RangeReplaceableCollection remove(at:)用法及代碼示例
- Swift RangeExpression ~=(_:_:)用法及代碼示例
- Swift Range overlaps(_:)用法及代碼示例
- Swift RangeReplaceableCollection append(_:)用法及代碼示例
- Swift RangeReplaceableCollection +=(_:_:)用法及代碼示例
- Swift Range用法及代碼示例
- Swift Range endIndex用法及代碼示例
- Swift RangeReplaceableCollection removeAll(where:)用法及代碼示例
- Swift Range ~=(_:_:)用法及代碼示例
- Swift RangeReplaceableCollection +(_:_:)用法及代碼示例
- Swift RangeExpression relative(to:)用法及代碼示例
- Swift Range isEmpty用法及代碼示例
- Swift RangeReplaceableCollection init(repeating:count:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 RandomAccessCollection subscript(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。