- 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.
推薦閱讀
- Hands-On Machine Learning with Microsoft Excel 2019
- Developing Mobile Games with Moai SDK
- 使用GitOps實(shí)現(xiàn)Kubernetes的持續(xù)部署:模式、流程及工具
- 深入淺出MySQL:數(shù)據(jù)庫(kù)開(kāi)發(fā)、優(yōu)化與管理維護(hù)(第2版)
- 數(shù)據(jù)驅(qū)動(dòng)設(shè)計(jì):A/B測(cè)試提升用戶(hù)體驗(yàn)
- 數(shù)亦有道:Python數(shù)據(jù)科學(xué)指南
- 大數(shù)據(jù)架構(gòu)商業(yè)之路:從業(yè)務(wù)需求到技術(shù)方案
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- Hadoop 3實(shí)戰(zhàn)指南
- 貫通SQL Server 2008數(shù)據(jù)庫(kù)系統(tǒng)開(kāi)發(fā)
- Node.js High Performance
- Scratch 2.0 Game Development HOTSHOT
- 云工作時(shí)代:科技進(jìn)化必將帶來(lái)的新工作方式
- MySQL 8.0從入門(mén)到實(shí)戰(zhàn)
- 2D 計(jì)算機(jī)視覺(jué):原理、算法及應(yīng)用