用法一
實例下標
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(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。