用法一
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"
可用版本
用法二
subscript(_:)
聲明
subscript(position: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Element { get }
Key
符合 Hashable
時可用。返回值
鍵和值對應於 position
的二元素元組。
參數
position
要訪問的鍵值對的位置。
position
必須是字典的有效索引並且不等於endIndex
。
概述
此下標將索引放入字典中,而不是鍵,並將相應的鍵值對作為元組返回。在執行將索引返回到字典中的基於集合的操作時,請將此下標與結果值一起使用。
例如,要在字典中查找特定值的鍵,請使用 firstIndex(where:)
方法。
let countryCodes = ["BR": "Brazil", "GH": "Ghana", "JP": "Japan"]
if let index = countryCodes.firstIndex(where: { $0.value == "Japan" }) {
print(countryCodes[index])
print("Japan's country code is '\(countryCodes[index].key)'.")
} else {
print("Didn't find 'Japan' as a value in the dictionary.")
}
// Prints "(key: "JP", value: "Japan")"
// Prints "Japan's country code is 'JP'."
可用版本
用法三
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(key: Key) -> Value? { get set }
Key
符合 Hashable
時可用。返回值
如果 key
在字典中,則與 key
關聯的值;否則,nil
。
參數
key
在字典中查找的關鍵。
概述
如果在字典中找到鍵,則此 key-based
下標返回給定鍵的值,如果未找到鍵,則返回 nil
。
以下示例創建一個新字典並打印在字典中找到的鍵 ("Coral"
) 和字典中未找到的鍵 ("Cerise"
) 的值。
var hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
print(hues["Coral"])
// Prints "Optional(16)"
print(hues["Cerise"])
// Prints "nil"
當您為鍵分配值並且該鍵已經存在時,字典會覆蓋現有值。如果字典不包含鍵,則鍵和值將作為新的鍵值對添加。
這裏,鍵 "Coral"
的值從 16
更新為 18
並為鍵 "Cerise"
添加新的鍵值對。
hues["Coral"] = 18
print(hues["Coral"])
// Prints "Optional(18)"
hues["Cerise"] = 330
print(hues["Cerise"])
// Prints "Optional(330)"
如果將 nil
指定為給定鍵的值,則字典會刪除該鍵及其關聯值。
在以下示例中,通過將 nil
分配給基於鍵的下標,從字典中刪除鍵 "Aquamarine"
的鍵值對。
hues["Aquamarine"] = nil
print(hues)
// Prints "["Coral": 18, "Heliotrope": 296, "Cerise": 330]"
可用版本
相關用法
- Swift Dictionary subscript(_:default:)用法及代碼示例
- Swift Dictionary suffix(from:)用法及代碼示例
- Swift Dictionary suffix(_:)用法及代碼示例
- Swift Dictionary sorted(by:)用法及代碼示例
- Swift Dictionary split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift Dictionary shuffled(using:)用法及代碼示例
- Swift Dictionary shuffled()用法及代碼示例
- Swift Dictionary allSatisfy(_:)用法及代碼示例
- Swift Dictionary values用法及代碼示例
- Swift Dictionary dropFirst(_:)用法及代碼示例
- Swift Dictionary firstIndex(where:)用法及代碼示例
- Swift Dictionary first(where:)用法及代碼示例
- Swift Dictionary merge(_:uniquingKeysWith:)用法及代碼示例
- Swift Dictionary isEmpty用法及代碼示例
- Swift Dictionary reduce(_:_:)用法及代碼示例
- Swift Dictionary init(uniqueKeysWithValues:)用法及代碼示例
- Swift Dictionary makeIterator()用法及代碼示例
- Swift Dictionary randomElement()用法及代碼示例
- Swift Dictionary init(grouping:by:)用法及代碼示例
- Swift Dictionary dropLast(_:)用法及代碼示例
- Swift Dictionary min(by:)用法及代碼示例
- Swift Dictionary max(by:)用法及代碼示例
- Swift Dictionary updateValue(_:forKey:)用法及代碼示例
- Swift Dictionary removeValue(forKey:)用法及代碼示例
- Swift Dictionary map(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Dictionary subscript(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。