> For the complete documentation index, see [llms.txt](https://gitbook.existorlive.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/swift/4.-ji-he-lei-xing/4.dictionary.md).

# Dictionary

元素为key-value对的集合类型

```swift
@frozen struct Dictionary<Key, Value> where Key : Hashable
```

Dictionary 是一种哈希表类型，提供对其记录的快速访问。表中的记录用 key 来标识，key 是一种 Hashable 类型。通过 key 可以获取到对应的 value，value 可以是任意类型。

## 1. 初始化，构造字典

```swift

// 初始化一个空的字典
var dic1 : [String:Int] = [:]

var dic2 = [String:Int]()

// 使用字面值初始化一个字典

var dic3 = ["He":1,"das":2]

```
