官术网_书友最值得收藏!

Dictionaries

Let's take a look at the definition of the Dictionary type:

public struct Dictionary<Key, Value> where Key : Hashable

In an array, you can only carry a single type of object, but there is no constraint on this type of object. Dictionaries can hold any Value, but the Key has to conform to the Hashable protocol. The reason is simple: in order to efficiently index a dictionary and compare two dictionary keys, Swift uses the hash value of the key, instead of the key itself. This also helps us to write a variety of dictionary types that can make our programs more expressive.

Many types conform to the Hashable protocol, and you can make your own types conform to this protocol if you need to create you own custom dictionary keys.

主站蜘蛛池模板: 普陀区| 循化| 西宁市| 新闻| 清镇市| 康马县| 新化县| 台州市| 嘉义县| 甘泉县| 新田县| 新巴尔虎右旗| 宾阳县| 江西省| 松桃| 方城县| 温宿县| 随州市| 榆社县| 涞源县| 永兴县| 若尔盖县| 正镶白旗| 卢氏县| 黄梅县| 读书| 高青县| 华坪县| 清涧县| 马公市| 友谊县| 南京市| 古丈县| 乳山市| 稷山县| 乌苏市| 竹北市| 博乐市| 扶风县| 兴文县| 江安县|