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

  • Hands-On Design Patterns with Swift
  • Florent Vilmart Giordano Scalzo Sergio De Simone
  • 132字
  • 2021-07-02 14:45:10

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.

主站蜘蛛池模板: 广水市| 扶沟县| 南乐县| 榆社县| 明光市| 尚志市| 慈溪市| 呼玛县| 孝义市| 三江| 合江县| 武陟县| 东光县| 双峰县| 多伦县| 和田县| 枞阳县| 吴旗县| 临泉县| 安仁县| 丰台区| 钦州市| 太谷县| 鄂州市| 阿坝县| 岑巩县| 巴里| 开远市| 南京市| 漳州市| 寿宁县| 湘潭市| 彝良县| 麻阳| 宜良县| 丹寨县| 九龙坡区| 昔阳县| 东丰县| 衡水市| 新河县|