- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 198字
- 2021-07-02 14:45:10
Initialization and mutability
Dictionaries are value types, and they follow the same mutability and initialization as other value types.
Let's use some important dates—the release dates for Swift—to work with dictionaries:
let swiftReleases = [
"2014-09-09": "Swift 1.0",
"2014-10-22": "Swift 1.1",
"2015-04-08": "Swift 1.2",
"2015-09-21": "Swift 2.0",
"2016-09-13": "Swift 3.0",
"2017-09-19": "Swift 4.0",
"2018-03-29": "Swift 4.1",
"2018-09-17": "Swift 4.2"
] // Swift release dates, source Wikipedia
As you can see, the dictionary type is quite simple; it's Dictionary<String, String>, which we can also denote as [String: String].
Similar to the Array and other value types, using the let keyword makes the dictionary immutable, which means the following:
- You cannot replace the contents of swiftReleases with a new dictionary
- You cannot add another key/value pair
- You cannot remove an existing key/value pair
- You cannot replace an existing key/value pair
- You cannot use any function or method that have an effect from above
While having the release date as a string is practical for writing the code, it becomes quite impractical if we want to do any kind of date comparison or computation.
In the next sections, we'll explore what we can do with dictionaries.
推薦閱讀
- LibGDX Game Development Essentials
- 數(shù)據(jù)挖掘原理與實踐
- 數(shù)據(jù)分析實戰(zhàn):基于EXCEL和SPSS系列工具的實踐
- Python金融大數(shù)據(jù)分析(第2版)
- WS-BPEL 2.0 Beginner's Guide
- Solaris操作系統(tǒng)原理實驗教程
- 深入理解InfluxDB:時序數(shù)據(jù)庫詳解與實踐
- 算力經(jīng)濟:從超級計算到云計算
- 數(shù)據(jù)庫原理與設(shè)計實驗教程(MySQL版)
- 領(lǐng)域驅(qū)動設(shè)計精粹
- NoSQL數(shù)據(jù)庫原理(第2版·微課版)
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)教程(第二版)
- 數(shù)據(jù)庫原理及應(yīng)用實驗:基于GaussDB的實現(xiàn)方法
- 從零進階!數(shù)據(jù)分析的統(tǒng)計基礎(chǔ)(第2版)
- 從運維菜鳥到大咖,你還有多遠II:企業(yè)數(shù)據(jù)中心建設(shè)及管理