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

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.

主站蜘蛛池模板: 晋城| 齐齐哈尔市| 旅游| 舟曲县| 通道| 岳池县| 浏阳市| 垦利县| 奇台县| 大新县| 永吉县| 资阳市| 杨浦区| 珲春市| 紫金县| 门头沟区| 凤台县| 萨迦县| 禹州市| 曲阜市| 甘德县| 紫云| 苍南县| 诏安县| 蕉岭县| 万州区| 沾益县| 平和县| 水城县| 尼木县| 上林县| 通河县| 重庆市| 徐水县| 龙海市| 湘阴县| 延川县| 南通市| 福清市| 阆中市| 札达县|