实例下标
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(_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。