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

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.

主站蜘蛛池模板: 深州市| 宁津县| 浪卡子县| 五华县| 迭部县| 绵阳市| 合肥市| 南召县| 吉安县| 南平市| 樟树市| 永清县| 澄迈县| 邹城市| 涟水县| 新民市| 宁夏| 临漳县| 宁陕县| 盐源县| 襄樊市| 治多县| 略阳县| 石楼县| 宜良县| 芜湖县| 宜州市| 胶州市| 轮台县| 佛冈县| 安泽县| 新宁县| 陆良县| 犍为县| 高陵县| 革吉县| 来安县| 平乐县| 汝城县| 白河县| 无棣县|