Google News
logo
Swift - Interview Questions
What are the different collection types available in Swift Programming Language?
There are two varieties of collection types in Swift:
 
Array : In Swift, you can create an array of single type or an array of multiple type.

Declare an empty array
Syntax :
let emptyIntArr:[Int] = []  
print(emptyIntArr)  

Output :

[]

 

Create an array directly : Swift is a type inference language so, we can also create an array directly without specifying the data type but we have to initialise with some values so that compiler can finds out its type.
 
Syntax :
let someIntArr = [1, 2, 3, 4, 5]  
print(someIntArr)  
Output :
[1, 2, 3, 4, 5]
 
Dictionary : In Swift, dictionary is similar to hash table in other programing language. You can store a key-value pair in a dictionary and access the value by using the key.

Declaring an empty dictionary : To create an empty dictionary, we specify the key:value Data type inside square brackets [].
 
Syntax :
let emptyDictionary:[Int:String] = [:]  
print(emptyDictionary) 
 
Output :
[:]
Declaring a dictionary with some values
let valDictionary = ["a":10, "b":20, "c":30, "d":40, "e":50, "f":60, "g":70, "h":80, "i":90]  
print(valDictionary)  
Output :
["c": 30, "d": 40, "g": 70, "b": 20, "a": 10, "f": 60, "h": 80, "i": 90, "e": 50]
Advertisement