DEV Community

Neeraj Gupta for DSC CIET

Posted on

Dictionaries in Swift

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

var <dictionary_name> : [< data type of key> : < data type of value> ]
Enter fullscreen mode Exit fullscreen mode

We can also store array in values

var <dictionary_name> : [< data type of key> : [ < data type of value> ] ]
Enter fullscreen mode Exit fullscreen mode

Few Examples on How to define dictionaries

Dictionaries without array of data types

var FinalExamScore : [String : Int] = ["Mathematics" : 80, "English" : 90, "Physics" : 70, "Chemistry" : 65]
Enter fullscreen mode Exit fullscreen mode

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 ( [ ] ).

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

FinalExamScore["Mathematics"] //80
Enter fullscreen mode Exit fullscreen mode

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

var SessionalScore : [String : [Int]] = ["Mathematics" : [50,80], "English" : [90,70]]
Enter fullscreen mode Exit fullscreen mode

This is how we define array of data type in value

Now, how we can acess these?

var SessionalScore : [String : [Int]] = ["Mathematics" : [50,80], "English" : [90,70]]
SessionalScore["Mathematics"] //[50,80]
SessionalScore["Mathematics"]![0] //50
Enter fullscreen mode Exit fullscreen mode

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

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

FinalExamScore["Social Science"]! //crash
Enter fullscreen mode Exit fullscreen mode

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

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
}
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

So, we confirmed that dictionaries are value types.

Hope, it help you understand dictionaries in Swift.

Top comments (1)

Collapse
 
tofulosophy profile image
tofulosophy

Hey, this was super informative! Thanks!