Dictionaries In Swift

 What’s a Dictionary?

                You can use a dictionary to store a collection of key-value information in your app. A dictionary is similar to an array in the sense that both store lists, also called collections, of items of the same type.


                A dictionary is a fundamental collection in Swift programming. With a dictionary you can store key-value data in your app. It’s a collection type, similar to array and sets, but completely different too.

Let's see an example:-








Here’s an example of a dictionary:
    
    










                The type of the scores dictionary is [String: Int]. That means that the type of the keys in the dictionary is String, and the type of its values is Int.

  In the above example we haven’t explicitly provided the type of scores, because Swift can infer it from the context of our code. Keep in mind though that every value in Swift has a type, even when it’s inferred!

A dictionary associates keys with values. Every key in a dictionary is unique. Every key-value pair in a dictionary needs to have the same types. Dictionary types, just like any other type in Swift, are fixed. You can’t change a dictionary’s type once it has been declared.

Creating a dictionary is straightforward. In the previous example we’ve used shorthand syntax. Like this:









The [...] is a dictionary literal. 

Here’s how you create an empty dictionary:




In the above code you’re initializing an empty dictionary of type [String: Int]. You can create an empty dictionary by writing its type followed by parentheses (). Again, the value is assigned to the scores constant.

Adding and Removing Dictionary Items:-
Before we continue, consider the fact that dictionaries are unordered. The key-value items in a dictionary do not have a default order or sorting. The association of key and value pairs is fixed, but their order within the dictionary is undetermined.

Arrays, on the other hand, are ordered. An array starts at index 0, and increments the index by one for every subsequent array element. The index-value pairs are fixed, which means that that arrays have a default order.

Add Elements to a Dictionary:-
We can add elements to a dictionary using the name of the dictionary with []. For example,






Output:-

Initial Dictionary:  ["India": "Delhi", "Pakistan", "Islamabad"]
Updated Dictionary:  ["India": "Delhi", "Pakistan": "Islamabad", "Japan": "Tokyo"]
In the above example, we have created a dictionary named capitalCity. Notice the line,

capitalCity["Japan"] = "Tokyo"
Here, we have added a new element to capitalCity with key: Japan and value: Tokyo

When you expect a dictionary to keep it’s order, and it doesn’t, you’re in for a treat (and a lot of bugs).
The array, dictionary and set collection types each have different properties and use cases, and it’s helpful to know when to use which type.
Let’s go back to the previous scores example.
 Here’s the relevant code:










Adding a new key-value pair to the scores dictionary is as simple as:



The above code adds a new key "Riya" to the scores dictionary with a value of 99. Note that their types are the same as the scores dictionary, i.e. strings for keys and integers for values.

The scores[...] part is called subscript syntax. It’s super helpful for quickly and concisely manipulating collections. And you can use it to get and set dictionary items.

Removing an element from a dictionary is done by setting it to nil. 
We use the removeValue() method to remove an element from the dictionary. 
Like this:



Getting and Changing Dictionary Items:-
                Let’s start by getting a value from a dictionary. You can do that by using its key, like this:






When you’re using a key that’s not present in the dictionary, the returned value is nil.

Here’s a comprehensive example:
    











                The value of score_bob is Optional(42), i.e. of type Int?, and the value of score_unknown is nil.

you change the elements of a dictionary with subscript syntax, too. Here’s how:












The value for the key "Suhana" has now been changed to 101. The syntax for adding, removing, changing and getting items from a dictionary is essentially the same!

Find Number of Dictionary Elements
We can use the count property to find the number of elements present in a dictionary. 

For example:



Output:-

3

Dictionaries in iOS Development:-

                It’s worth noting here that dictionaries in Swift are based on the principles of a so-called hash table. In short, a hash table can associate hashes with values, in a computer’s memory. Your dictionary keys are translated to hashes with a hash function, which is essentially the same as translating a complex phone number to a small integer value. This integer value is then stored in a lookup table.

                When you access a dictionary by its key, the key gets translated using that hash function, and looked up in the table. This is doesn’t require any additional memory per dictionary item, and retrieving an item from the dictionary can be done without going through the entire lookup table one-by-one. This is very efficient and fast.

Comments

Popular posts from this blog

POWER BI

Face Recognition

BOOTSTRAP