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

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.

主站蜘蛛池模板: 泰兴市| 普洱| 南开区| 思南县| 澜沧| 突泉县| 黎川县| 樟树市| 南丰县| 衡南县| 阆中市| 南华县| 镇远县| 石泉县| 安岳县| 泗水县| 泗水县| 福州市| 惠安县| 河西区| 芷江| 柞水县| 聊城市| 图们市| 石家庄市| 马关县| 济南市| 无极县| 临洮县| 郑州市| 宁陵县| 东乡| 随州市| 清河县| 周口市| 姚安县| 门头沟区| 齐河县| 临武县| 和平区| 淮北市|