用法一
subscript(_:)
聲明
subscript(bounds: Range<Self.Index>) -> Slice<Self> { get set }
參數
bounds
一係列集合的索引。範圍的邊界必須是集合的有效索引。
概述
訪問的切片對與原始集合相同的元素使用相同的索引。始終使用切片的 startIndex
屬性,而不是假設其索引從特定值開始。
此示例演示獲取字符串數組的切片,查找切片中字符串之一的索引,然後在原始數組中使用該索引。
var 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
streets[index!] = "Eustace"
print(streets[index!])
// Prints "Eustace"
可用版本
用法二
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
可用版本
用法三
subscript(_:)
聲明
subscript(position: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Values.Element { get set }
參數
position
要訪問的元素的位置。
position
必須是不等於endIndex
屬性的集合的有效索引。
概述
例如,您可以使用其下標替換數組的元素。
var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
streets[1] = "Butler"
print(streets[1])
// Prints "Butler"
您可以使用集合的結束索引以外的任何有效索引為集合下標。結束索引是指在集合的最後一個元素之後的位置,因此它與元素不對應。
可用版本
用法四
subscript(_:)
聲明
subscript(bounds: Range<Self.Index>) -> Slice<Self> { get }
SubSequence
為 Slice<Self>
時可用。參數
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"
可用版本
相關用法
- Swift Dictionary.Values suffix(_:)用法及代碼示例
- Swift Dictionary.Values suffix(from:)用法及代碼示例
- Swift Dictionary.Values split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift Dictionary.Values starts(with:)用法及代碼示例
- Swift Dictionary.Values split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift Dictionary.Values sorted()用法及代碼示例
- Swift Dictionary.Values shuffled(using:)用法及代碼示例
- Swift Dictionary.Values shuffled()用法及代碼示例
- Swift Dictionary.Values sorted(by:)用法及代碼示例
- Swift Dictionary.Values min(by:)用法及代碼示例
- Swift Dictionary.Values prefix(upTo:)用法及代碼示例
- Swift Dictionary.Values min()用法及代碼示例
- Swift Dictionary.Values firstIndex(of:)用法及代碼示例
- Swift Dictionary.Values first用法及代碼示例
- Swift Dictionary.Values compactMap(_:)用法及代碼示例
- Swift Dictionary.Values contains(_:)用法及代碼示例
- Swift Dictionary.Values isEmpty用法及代碼示例
- Swift Dictionary.Values endIndex用法及代碼示例
- Swift Dictionary.Values dropLast(_:)用法及代碼示例
- Swift Dictionary.Values firstIndex(where:)用法及代碼示例
- Swift Dictionary.Values flatMap(_:)用法及代碼示例
- Swift Dictionary.Values index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift Dictionary.Values allSatisfy(_:)用法及代碼示例
- Swift Dictionary.Values enumerated()用法及代碼示例
- Swift Dictionary.Values description用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Dictionary.Values subscript(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。