Skip to main content

How to use Dictionary in Swift

Swift dictionary are specific about the types of keys and values they can store the number.They differ from
Objective C 's NSDictionary and NSMutableDictionary classed ,which can use any kind of object
as their key and values and do not provide any information about the nature of these object .in Swift ,the type of key and values that a particular dictionary can store is alway made clear ,either though an explicit type annotation od through type differenceswift's dictionary type is write as Dictionary <Key type,valuetype > where key type is kind of value that can be used as a dictionary key and Value type is type of value that the dictionary stores for those key

Dictionary literal

we can initialize a dictionary with dictionary literal,which has similar syntax to the array literal seen earlier .A dictionary literal is a shorthand write one or more key value pairs as a dictionary collection

dictionary has a key and value in a dictionary in a dictionary literal the key and value in each key value pair are separated by colon, the key-value pairs are written a list separated by commas ,surround by pair of square brackets:
[key1:value 1 ,key2:value 2, key 3 value 3]
the example below create a dictionary to store the names of smart phone idevce this dictionary:
var idevice :[String:Int] = ["I phone 7 plus":1,"I phone 6s plus":2]

Note 1 :you can use with var idvice:[String:AnyObject] = ["I phone 7 plus":1 as AnyObject,"I phone 6s plus":2 as AnyObject] with instance you can add any kind of value
such as : int, string float ,char etc
Note 2 :you can use method like this Dictionary<Int,String>() instead of
the idevce dictionary is initialize with a dictionary is initialized with a dictionary literal containing two key-value pairs the fist pair has a key of "I phone 7 plus" and value of "1" the second pair has a key of " i Phone 6 s plus"and value of "2".

Accessing and modifying a Dictionary
Like an array ,you can find out the number of items in a Dictionary by checking its read only count property:print("we have (idevice.count)device " )//we have 2device you can add new item to a dictionary with syntax:


idvice["i phone SE"] = 3

you can also use syntax to change the value associate with a particular key :

idvice["i phone SE"] = 4

To remove an item in the dictionary with the method:
idvice.removeValue(forKey: "I phone 7 plus")

Integrating over a Dictionary

you can integrate over the key- value pairs in dictionary with a for-in loop each item in the dictionary is returned as a (key,value) tuple, and you can decompose the tuple's members into temporary constants or variable as part of the iteration:

for (vl ,k)in idvice{
    print(k);
    print(vl);

}

Creating an Empty Dictionary

As with array ,you can create an empty Dictionary syntax:


var nameofBook = Dictionary<Int,String>()




so this end of my blog i hope that it help you learn alot please post any question and comment right herei'm kevin see you next time







Comments

Popular posts from this blog

When is viewWillAppear, viewDidAppear, viewDidLoad, viewWillDisappear actually called

A few months ago I decided to apply for a job at a software company, I interviewed for IOS developer position. After a week I got a result they  gave me an offer 400$ per months, salary not too high, not too low for me  I also  struggled with a lot of questions from my interviewer, so today in this article I will share with you my experiment to answer the question from the interviewer  if you are  IOS developer you are familiar with  viewWillAppear , viewDidAppear , viewDidLoad , viewWillDisappear , and viewDidDisappear  let me ask you a question what is the running order of viewDidAppear , viewDidLoad , viewWillDisappear , and viewDidDisappear. This question sounds easy it’s straightforward. Actually, it very hard to answer, because when you make app maybe you don't care about it , No one tells you before you get an interview ,  now we need to create a new project, inside of ViewController.swift  we need to implement some method...

Build Carousel Effect in iOS with Swift Kien Pham

This tutorial I’m going to show you how to build Carousel effect it is the most common, you can see it everywhere you can see it on Instagram or App Store which is beautiful UI you can use a lot of in your app many places  that you want to show beautiful text and beautiful image view First thing first we need to create a new project and select iOS->SingleView Application Product Name: Tableview – this the name of your project ·      Company Identifier: KienPhamDev It’s actually the domain name written the other way round. if  you have a domain, you can use your own domain name otherwise you may use mine or just fill in ·      Device family: you can choose universal it’s not important ·      Choose swift as Language And then we are going to the main storyboard , in this tutorial we want to use ViewController so we just need to drag a new Collection View from Object library ...

How to update two images using GCD in swift

If you are IOS developer you can run into a problem when your app download a lot of image from URL maybe your app is crashed or you can't interact with the rest of the application, when I interviewed for a job  a few months ago, I also got this question from my interviewer   He assumed that there are 3 images from URL when user click on the update button all of them will be updated at  the same time so let’s do it a demo to know what happens when your application run without multithreading    Inside of my project I need  some element into ViewController,  As you can see we have 2 images and a button to update our image  Inside of ViewController , we need to hook up all of them with  @IBOutlet and @IBAction it should look like this  As you can see when user click on the update button to download two images from URL, you can't interact with the rest of the application you can’t d...