Dictionaries and Optional in Swift


The fact that Swift is a new language for the developer. The most widely recognized obstacle engineers keep running into is understanding. In this post, I will describe Dictionaries and  Optional in Swift. What are they?


It will good to cover my previous articles before this. Its help to learn more about IOS development. In which I explained.Day one training in ios.

You will learn in this section

  1. What is Dictionaries
  2. What is optional

What is Dictionaries

The swift provides three main types of collection such as set array and dictionary. The dictionary is a collection of value . They store unorder of value with the same type. They also strick the user does not enter the wrong value. The dictionary can be string or integer but I will be unique in a dictionary.

The dictionaries always lie in the form of key and its values. Its different from sets and arrays. The sets represent the different value. Each set has different values. While the array represents the values with index.

Every element in the array has an index which approaches the element. In the dictionary have a million keys and every key represent a value.

The is the simple way to declare.

We will create the other dictionary that contains two string. Every dictionary contains the key and value. The kay that is completed is separated by the comma. Every key is separate with value with a colon. You can also assign the null value to the dictionary.

There is a simple way to check all the dictionary. Using for loop we can check all the element in the array.

If you want to find just key in the dictionary than apply this syntax.

if you want just find just value form dictionary than apply this.

The concept of the dictionary is the same Jason ( javascript object notation). The Jason information comprises of <object> and <array>. The information in an archive is the type of key and esteem combine.

The information is isolated by commas. The curly section { } hold objects and Box section [] array. This the dictionary same like key and value some of key contain the string value some of containing an integer value.

What is optional

In swift 4 optional are used for the absence of value. The idea behind the optional is what is in the box. Is the box is empty or some value lies in the box. The optional check how many elements in the array or box. To declare an optional, we utilize a question mark. The question mark shows that we are managing an optional.

Declaring Optionals

They are two type of optional sign that is commonly used in swift which is <? or !>.

  • var MyData: String!
  • var MyData: String?

The optional that used with ! sign also called implicit unwrapped optional. The type option grantee you have data before execution. while the optional with? the sign has not grantee the data at runtime. The following is the simple way to declare optional with three different way with integer value with string and with nill value.

Optional Unwrapping

you cannot access the value of optional variable same like variable and constant. The optional unwrapping means when you declare a variable is optional and want to use in a program then its mandatory to use the exclamation mark at last of the variable.

Unwrapping Automatic

You can declare an optional variable with exclamation rather than a question mark. Such discretionary factors will unwrap consequently and you don’t have to utilize any further outcry stamp toward the finish of the variable to get the doled out esteem.

Optional Binding

Optional binding means you just use an optional with if and else condition. You to want to check the decision on boolean.


In this post divided into two part dictionary and optional in swift. I had describes all the concepts that are used in a dictionary. My main objective that had achieved how to declare the dictionary how to access the key and value. In separately I explain how can access the key and value. The second part is optional which contain what are optional where optional is used. The difference between the unwrapped option and optional binding with help of an example. For Learn More about.

Previous articleAndroid Login Window Complete Example
Next articleAndroid Customized ProgressBar Example
My name is Muhammad Zubair. I want to help you a lot of things that a blog provide. You feel free to ask the question anytime you might have! While most recently, he tends to share her wealth of knowledge and expertise on SEO, link building, inbound marketing,