Swift Program to Use Different Types of a Collection


A collection is a group of elements or objects that are gathered together for some specific tasks. Swift supports three types of collections: Array, Set, and Dictionary. They are implemented as generic collections, also they are clear about what type of values they can store which means you can not store the wrong type of values in the collections.

Array

It is an ordered collection which is used to store similar types of data or elements. It can store duplicate values. It is both mutable and immutable.

Syntax

var arr :[Type] = [E1, E2, E3]

var arr = [V1, V2, V3]

Here, arr is the name of the array, and Type is the data type or the type of data that will be stored in the array like Int, String, and E1, E2,.. are the elements of the array.

Set

It is an unordered collection which is used to store unique values of similar data types. It is both mutable and immutable.

Syntax

var sName : Set = [W1, W2, W3]

var sName : Set<Type> = [X1, X2, X3]

Here, sName is the name of the set, and Type is the data type or the type of data that will be stored in the set like Int, String, and W1, W2,.. are the elements of the set.

Dictionary

It is an unordered collection which is used to store data in key-value pairs. Here the key must be unique. It is both mutable and immutable.

Syntax

var dName = [K1:V1, K2:V2, K2:V2]

var dName:[KType:VType] = [K1:V1, K2:V2, K2:V2]

Here, dName is the name of the dictionary, K1 is the key and V1 are the values of the dictionary. KType is the data type of keys and VType is the data type of values, they can be the same or different.

Algorithm

  • Step 1 − Create a dictionary.

  • Step 2 − Create an array of integer types.

  • Step 3 − Create a set of integer types.

  • Step 4 − Run a for-in loop to iterate through each element of the collection.

  • Step 5 − Print the output.

Example

In the following Swift program, we will use different types of collections like arrays, sets and dictionaries. So for that, we create a dictionary, array and set collection and then print each element from these collections by iterating over them using a for-in loop.

import Foundation
import Glibc

// Create a dictionary
var myDict = [1: "Boat", 2: "Box", 3: "Bucket", 4: "Boy"]

print("Dictionary:")

// Iterating over dictionary
for (x, y) in myDict{
  print("Key:\(x) and value: \(y)")
}

// Create an array
var myArr = [2, 4, 33, 11, 10, 18, 38]

print("\nArray:")

// Iterating over array
for x in myArr{
    print(x)
}


// Create a set
var mySet: Set = [3, 8, 99, 33, 71]

print("\nSet:")

// Iterating over set
for y in mySet{
  print(y)
}

Output

Dictionary:
Key:2 and value: Box
Key:1 and value: Boat
Key:4 and value: Boy
Key:3 and value: Bucket

Array:
2
4
33
11
10
18
38

Set:
3
8
33
71
99

Conclusion

So this is how we use different types of a collection. You are allowed to perform various operations on the data present in these collections like accessing, modifying, deleting, etc. Every collection has its own specifications, you can use them according to your requirement such as if you want to store the marks of students, then you can use an array. If you want to store only unique values, then you can use Set. Similarly, if you want to store the data with a unique id then you can use a dictionary.

Updated on: 16-Jun-2023

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements