用法一
實例下標
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"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
實例下標
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(bounds: Range<EmptyCollection<Element>.Index>) -> EmptyCollection<Element>.SubSequence { get set }
參數
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 EmptyCollection suffix(_:)用法及代碼示例
- Swift EmptyCollection suffix(from:)用法及代碼示例
- Swift EmptyCollection starts(with:)用法及代碼示例
- Swift EmptyCollection sorted(by:)用法及代碼示例
- Swift EmptyCollection sort()用法及代碼示例
- Swift EmptyCollection shuffle()用法及代碼示例
- Swift EmptyCollection shuffled(using:)用法及代碼示例
- Swift EmptyCollection shuffle(using:)用法及代碼示例
- Swift EmptyCollection shuffled()用法及代碼示例
- Swift EmptyCollection split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift EmptyCollection sorted()用法及代碼示例
- Swift EmptyCollection sort(by:)用法及代碼示例
- Swift EmptyCollection split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift EmptyCollection first(where:)用法及代碼示例
- Swift EmptyCollection index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift EmptyCollection partition(by:)用法及代碼示例
- Swift EmptyCollection prefix(upTo:)用法及代碼示例
- Swift EmptyCollection dropFirst(_:)用法及代碼示例
- Swift EmptyCollection enumerated()用法及代碼示例
- Swift EmptyCollection filter(_:)用法及代碼示例
- Swift EmptyCollection prefix(_:)用法及代碼示例
- Swift EmptyCollection randomElement(using:)用法及代碼示例
- Swift EmptyCollection allSatisfy(_:)用法及代碼示例
- Swift EmptyCollection reverse()用法及代碼示例
- Swift EmptyCollection forEach(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 EmptyCollection subscript(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。