O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required