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

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.

主站蜘蛛池模板: 衡阳市| 中超| 鄂温| 朝阳市| 河西区| 英山县| 保亭| 怀宁县| 罗甸县| 金堂县| 荔浦县| 宣汉县| 大同市| 峨眉山市| 乌恰县| 永善县| 南投县| 洞口县| 信宜市| 阿鲁科尔沁旗| 西平县| 靖宇县| 武安市| 全州县| 云林县| 安仁县| 林甸县| 惠东县| 昆山市| 永兴县| 定西市| 肇州县| 莱芜市| 昌图县| 芜湖市| 苍溪县| 东宁县| 莱西市| 罗平县| 绥芬河市| 石屏县|