用法一
实例下标
subscript(_:)
访问由范围表达式指定的集合元素的连续子范围。
声明
subscript<R>(r: R) -> Self.SubSequence where R : RangeExpression, Self.Index == R.Bound { get }
参数
bounds
一系列集合的索引。范围的边界必须是集合的有效索引。
概述
范围表达式转换为相对于该集合的具体子范围。例如,将 PartialRangeFrom
范围表达式与数组一起使用会访问从范围表达式开头到数组结尾的子范围。
let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2...]
print(streetsSlice)
// ["Channing", "Douglas", "Evarts"]
访问的切片对与原始集合使用的相同元素使用相同的索引。此示例在 streetsSlice
中搜索切片中的字符串之一,然后在原始数组中使用该索引。
let index = streetsSlice.firstIndex(of: "Evarts") // 4
print(streets[index!])
// "Evarts"
始终使用切片的 startIndex
属性,而不是假设其索引从特定值开始。尝试使用切片索引范围之外的索引访问元素可能会导致运行时错误,即使该索引对原始集合有效。
print(streetsSlice.startIndex)
// 2
print(streetsSlice[2])
// "Channing"
print(streetsSlice[0])
// error: Index out of bounds
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
实例下标
subscript(_:)
访问集合元素的连续子范围。
声明
subscript(r: Range<String.UTF8View.Index>) -> String.UTF8View.SubSequence { get }
参数
bounds
一系列集合的索引。范围的边界必须是集合的有效索引。
概述
例如,将 PartialRangeFrom
范围表达式与数组一起使用会访问从范围表达式开头到数组结尾的子范围。
let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2..<5]
print(streetsSlice)
// ["Channing", "Douglas", "Evarts"]
访问的切片对与原始集合相同的元素使用相同的索引。此示例在 streetsSlice
中搜索切片中的字符串之一,然后在原始数组中使用该索引。
let index = streetsSlice.firstIndex(of: "Evarts")! // 4
print(streets[index])
// "Evarts"
始终使用切片的 startIndex
属性,而不是假设其索引从特定值开始。尝试使用切片边界之外的索引访问元素可能会导致运行时错误,即使该索引对原始集合有效。
print(streetsSlice.startIndex)
// 2
print(streetsSlice[2])
// "Channing"
print(streetsSlice[0])
// error: Index out of bounds
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+, Swift 4.0+
用法三
实例下标
subscript(_:)
访问给定位置的代码单元。
声明
subscript(i: String.UTF8View.Index) -> UTF8.CodeUnit { get }
参数
position
视图的有效索引。
position
必须小于视图的结束索引。
概述
以下示例使用下标打印字符串的第一个 UTF-8 代码单元的值。
let greeting = "Hello, friend!"
let i = greeting.utf8.startIndex
print("First character's UTF-8 code unit: \(greeting.utf8[i])")
// Prints "First character's UTF-8 code unit: 72"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift String.UTF8View suffix(from:)用法及代码示例
- Swift String.UTF8View suffix(_:)用法及代码示例
- Swift String.UTF8View split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代码示例
- Swift String.UTF8View split(separator:maxSplits:omittingEmptySubsequences:)用法及代码示例
- Swift String.UTF8View starts(with:)用法及代码示例
- Swift String.UTF8View sorted()用法及代码示例
- Swift String.UTF8View shuffled(using:)用法及代码示例
- Swift String.UTF8View shuffled()用法及代码示例
- Swift String.UTF8View sorted(by:)用法及代码示例
- Swift String.UTF8View first用法及代码示例
- Swift String.UTF8View elementsEqual(_:)用法及代码示例
- Swift String.UTF8View last(where:)用法及代码示例
- Swift String.UTF8View debugDescription用法及代码示例
- Swift String.UTF8View isEmpty用法及代码示例
- Swift String.UTF8View randomElement(using:)用法及代码示例
- Swift String.UTF8View allSatisfy(_:)用法及代码示例
- Swift String.UTF8View last用法及代码示例
- Swift String.UTF8View reduce(_:_:)用法及代码示例
- Swift String.UTF8View prefix(_:)用法及代码示例
- Swift String.UTF8View index(_:offsetBy:limitedBy:)用法及代码示例
- Swift String.UTF8View lastIndex(where:)用法及代码示例
- Swift String.UTF8View contains(where:)用法及代码示例
- Swift String.UTF8View max(by:)用法及代码示例
- Swift String.UTF8View flatMap(_:)用法及代码示例
- Swift String.UTF8View prefix(through:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 String.UTF8View subscript(_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。