Dictionaries in Swift
Tags: basics, swift, apple, beginners
Published: 2020-12-06 15:42:19

# Brief Introduction About Swift

Swift is a language developed by Apple which is made using modern approach, include safety features and software design patterns. As the name suggests swift is fast, safe and easy to use. It is basically made a replacement for c-based family(C, C++ and Objective-C). It can be used to make apps and can be also used for cloud services and it is among the fastest growing languages.

# Dictionaries

## What are Dictionaries

Doctionaries are the ones which have key value pair that is every value is mapped to it's corresponding key and they are very fast to acess so they are used in API's as API's generally is a time consuming process but when we get data from database then we can acess that data very fast as they are stored in key value pairs.

## How to define Dictionary

```swift
var <dictionary_name> : [< data type of key> : < data type of value> ]
```

We can also store array in values

```swift
var <dictionary_name> : [< data type of key> : [ < data type of value> ] ]
```
## Few Examples on How to define dictionaries

### Dictionaries without array of data types

```swift
var FinalExamScore : [String : Int] = ["Mathematics" : 80, "English" : 90, "Physics" : 70, "Chemistry" : 65]
```
Now, we defined a dictionary but now we want to get value from it. We can do that by subscripting that is passing key in square brackets ( **[ ]** ).

```swift
var FinalExamScore : [String : Int] = ["Mathematics" : 80, "English" : 90, "Physics" : 70, "Chemistry" : 65]

FinalExamScore["Mathematics"] //80
```

### Dictionaries with array of data types

Now, we saw how we can define dictionaries with data types only. Now, what if we want to define array of data types

```swift
var SessionalScore : [String : [Int]] = ["Mathematics" : [50,80], "English" : [90,70]]
```
This is how we define array of data type in value

Now, how we can acess these?

```swift
var SessionalScore : [String : [Int]] = ["Mathematics" : [50,80], "English" : [90,70]]
SessionalScore["Mathematics"] //[50,80]
SessionalScore["Mathematics"]![0] //50
```
So, this is how we acess these values. **Now always take care while force unwrapping as it can crash your app** like in below example

```swift
var FinalExamScore : [String : Int] = ["Mathematics" : 80, "English" : 90, "Physics" : 70, "Chemistry" : 65]

FinalExamScore["Social Science"]! //crash
```
Now here we were saying that get key **Social Science** and we are force unwrapping and saying swift that i guarantee that there is a key with this name. But as it was not there so app crashed

We can overcome this by putting a check here and also this is a good practice

```swift
var FinalExamScore : [String : Int] = ["Mathematics" : 80, "English" : 90, "Physics" : 70, "Chemistry" : 65]

if FinalExamScore["Social Science"] != nil{
    FinalExamScore["Social Science"]
} else {
    print("No such key exist") // This statement will run
}
```
# Are Dictionaries Reference Type or Value Type

Dictionaries are value type means they always create seperate copies rather than referencing to other dictionaries in case of copying one dictionary to other and let's confirm that in code.

```swift
var FinalExamScore : [String : Int] = ["Mathematics" : 80, "English" : 90, "Physics" : 70, "Chemistry" : 65]

var ExamScore = FinalExamScore

ExamScore["Mathematics"] // 80
FinalExamScore["Mathematics"] //80

ExamScore["Mathematics"] = 90

ExamScore["Mathematics"] //90
FinalExamScore["Mathematics"] //80
```

So, we confirmed that dictionaries are value types.

Hope, it help you understand dictionaries in Swift.