Collection Types In Swift

Introduction

 
Swift has two types of collections namely arrays and dictionaries for storing collections of values. An array stores an ordered list of values of the same type. Dictionaries store unordered collections of values of the same type. The insertion of the wrong type into an array is not possible. Swift's use of explicitly typed collections ensures the code is always clear about the types of values it works with and enables it to catch any type of mismatches in code development.
 

Arrays

 
An array stores multiple values of the same type in ordered list and the same value can appear in an array multiple times at different positions. A swift array is specific about the kinds of values they store. It can store any kind of object and do not provide any information about the nature of objects. The type of values that a particular array can store is always made clear. Array <type> where type is the type that the array is allowed to store. 
 

Array literals

 
The initialization of an array with an array is literal to write one or more values as an array collection. An array literal is written as a list of values, separated by commas surrounded by a pair of square brackets. In the below example the list is a variable declared as an array of string values and it has the values stored in it.
  1. var list: String[] = ["book","pen"]   

Accessing and Modifying an Array

 
Accessing and modifying an array can be done by method and properties using subscript syntax. The boolean isEmpty property used for checking the count property is equal to 0. The subscript syntax is used to change the range of values, even the replacement set of values has a different length than the range that it is replacing.
  1. if shoppingList.isEmpty  
  2. {  
  3.     println ("The List is empty")  
  4. }  
  5. else  
  6. {  
  7.     println("The List is not empty")  
  8. }  
To insert an item into the array at a specified index, call the array's insert method: shoppingList.insert ("syrup", atIndex : 0). This call to insert method inserts a new item with a value of "syrup" at the beginning of the list and it is indicated by an index of 0. The removeAtIndex method is used to remove an item from the array and returns the removed item. To remove the final item in the array, the removeLast method rather than the removeAtIndex method to avoid the need to query the array count property.
 

Iterating over an Array

 
The entire set of values can be iterated in an array with the for-in loop. The global enumerate function is used to iterate over the array is used to get the integer index of each item and its value.
  1. for item in shoppinglist  
  2. {  
  3.     println(item)  
  4. }  
  5.   
  6. //items  
  7.   
  8. for (index, value) in enumerate (shoppingList)  
  9. {  
  10.     println ("Item \(index + 1):\(value)")  
  11. }  

Dictionaries

 
The dictionary is a container that stores multiple values of the same type. Each value is associated with a unique key that acts as an identifier for value within the directory. Items in a dictionary do not have a specified order. Dictionary is used to lookup values on their identifier and swift dictionaries are specific about the type of keys and values is stored.
 
The type of keys and values that a particular dictionary can store. Swift dictionary type is written as Dictionary <key type, value type> , key type is a type of value that can be used as a dictionary key, and value type is a type of value that dictionary stores for those keys.
  1. var airport : Dictionay <Strig, String> = ["MUM:mumbai","CHE:chennai"]  
The dictionary literal contains two string: String pairs. This matches the type of airport variable declaration and the assignment of dictionary literal is permitted as a way to initialize the airport dictionary with two initial items.
 

Iterating over a Dictionary

 
The key-value pairs in dictionary can be iterated within a for-in loop. Each item in the dictionary is returned as (key, value) tuple, decompose the tuple members into temporary constants or variables.
  1. for (airportCode, airportName) in airports    
  2. {    
  3.     println("\(airportCode):\(airportName)")    
  4. }    
Arrays and directories store multiple values together in a single collection. The collection created is mutable when creating an array or directory and assign it to a variable. The use of a collection can be changed after created by adding more items to collection or removing existing items.