# 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]

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/swift/4.-ji-he-lei-xing/4.dictionary.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
