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

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.

主站蜘蛛池模板: 罗城| 宁海县| 从化市| 郸城县| 海宁市| 西和县| 民权县| 河曲县| 两当县| 中超| 绩溪县| 铜川市| 海门市| 武穴市| 玛纳斯县| 明光市| 宜兴市| 莱州市| 张家界市| 喜德县| 当雄县| 黎平县| 南郑县| 广南县| 盈江县| 泰安市| 上思县| 静宁县| 砚山县| 鹰潭市| 阿勒泰市| 桂平市| 沛县| 清苑县| 裕民县| 剑河县| 合肥市| 平谷区| 泸水县| 淮北市| 泸州市|