- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 124字
- 2021-07-02 23:54:25
Collections
Swift provides typed collections such as array, dictionaries, and sets. In Swift, unlike Objective-C, all elements in a collection will have the same type, and we will not be able to change the type of a collection after defining it.
We can define collections as immutable with let and mutable with var, as shown in the following example:
// Arrays and Dictionaries
var cheeses = ["Brie", "Tete de Moine", "Cambozola", "Camembert"]
cheeses[2] = "Roquefort"
var cheeseWinePairs = [
"Brie":"Chardonnay",
"Camembert":"Champagne",
"Gruyere":"Sauvignon Blanc"
]
cheeseWinePairs ["Cheddar"] = "Cabarnet Sauvignon"
// To create an empty array or dictionary
let emptyArray = [String]()
let emptyDictionary = Dictionary<String, Float>()
cheeses = []
cheeseWinePairs = [:]
The for-in loops can be used to iterate over the items in collections.
推薦閱讀
- GitHub Essentials
- 數據庫應用實戰
- 數據庫技術與應用教程(Access)
- InfluxDB原理與實戰
- Voice Application Development for Android
- Oracle RAC 11g實戰指南
- WS-BPEL 2.0 Beginner's Guide
- 數據科學工程實踐:用戶行為分析與建模、A/B實驗、SQLFlow
- R語言數據挖掘
- 數字IC設計入門(微課視頻版)
- SIEMENS數控技術應用工程師:SINUMERIK 840D-810D數控系統功能應用與維修調整教程
- 數據指標體系:構建方法與應用實踐
- MySQL核心技術手冊
- Flume日志收集與MapReduce模式
- 數據庫高效優化:架構、規范與SQL技巧