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]
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>()
i'm kevin see you next time
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".
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:
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>()
i'm kevin see you next time
Comments
Post a Comment