- 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.
推薦閱讀
- 數據庫應用實戰
- SQL Server 2012數據庫技術與應用(微課版)
- Effective Amazon Machine Learning
- SQL查詢:從入門到實踐(第4版)
- Mastering Machine Learning with R(Second Edition)
- 數據庫原理與應用(Oracle版)
- 大數據精準挖掘
- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- 數據庫應用系統開發實例
- 中文版Access 2007實例與操作
- 區塊鏈+:落地場景與應用實戰
- 數據挖掘算法實踐與案例詳解
- AI Crash Course
- ORACLE 11g權威指南
- Mastering Java for Data Science