實例下標
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 BidirectionalCollection suffix(_:)用法及代碼示例
- Swift BidirectionalCollection last(where:)用法及代碼示例
- Swift BidirectionalCollection lastIndex(where:)用法及代碼示例
- Swift BidirectionalCollection lastIndex(of:)用法及代碼示例
- Swift BidirectionalCollection indices用法及代碼示例
- Swift BidirectionalCollection index(_:offsetBy:)用法及代碼示例
- Swift BidirectionalCollection reversed()用法及代碼示例
- Swift BidirectionalCollection joined(separator:)用法及代碼示例
- Swift BidirectionalCollection index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift BidirectionalCollection dropLast(_:)用法及代碼示例
- Swift BidirectionalCollection last用法及代碼示例
- Swift BinaryInteger quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift BinaryInteger trailingZeroBitCount用法及代碼示例
- Swift BinaryInteger init(clamping:)用法及代碼示例
- Swift BinaryInteger <<=(_:_:)用法及代碼示例
- Swift BinaryInteger !=(_:_:)用法及代碼示例
- Swift BinaryInteger /(_:_:)用法及代碼示例
- Swift BinaryInteger init(truncatingIfNeeded:)用法及代碼示例
- Swift BinaryInteger ==(_:_:)用法及代碼示例
- Swift BinaryFloatingPoint binade用法及代碼示例
- Swift BinaryInteger init(_:)用法及代碼示例
- Swift BinaryInteger %=(_:_:)用法及代碼示例
- Swift BinaryFloatingPoint random(in:using:)用法及代碼示例
- Swift BinaryInteger &=(_:_:)用法及代碼示例
- Swift BinaryInteger |(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 BidirectionalCollection subscript(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。