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

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.

主站蜘蛛池模板: 五常市| 邢台县| 宿州市| 徐闻县| 德令哈市| 铅山县| 开鲁县| 苗栗市| 驻马店市| 溧阳市| 天祝| 云浮市| 酉阳| 鄯善县| 延津县| 宝坻区| 荥阳市| 共和县| 江城| 普陀区| 上栗县| 楚雄市| 白山市| 丰城市| 鄂州市| 武隆县| 太白县| 湾仔区| 孟连| 民丰县| 洞口县| 丰城市| 宜兴市| 莱州市| 开化县| 大邑县| 安塞县| 壤塘县| 开封县| 吴江市| 淮安市|