-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-dictionary-collection.swift
55 lines (40 loc) · 1.38 KB
/
13-dictionary-collection.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var dict = ["2":"two", "3": "three" ]
print(dict)
dict["5"]="5"
dict["3"]="03"
print(dict)
dict.updateValue("four", forKey: "4")
print(dict)
//============================================================
// Dictionary (key, value pair) and Set {Unordered Collection}
var dictFiles = ["pdf":"Portable Document Format","doc":"Microsoft Word Document","exe":"Windows Executable"]
print(dictFiles)
var dictRStation = [Int:String]()
// inserting/updating values in dictionary
dictRStation[10] = "Ludhiana"
dictRStation[8] = "Jalandhar"
dictRStation[11] = "Patiala"
dictRStation[8] = "Phagwara" // updating values
// inserting/updating values using updateValue Method
var orig = dictRStation.updateValue("Canada",forKey: 10) // String (as per Value data type) is the return type of updateValue method
print(orig ?? "") // String?
orig = dictRStation.updateValue("UK",forKey: 12)
print(orig ?? "No original Value available")
// remove/delete values from dictionary using removeValue Method
var orig1 = dictRStation.removeValue(forKey: 9) //returns the value which is being deleted
print(orig1 ?? "")
print(dictRStation)
// remove/delete values from dictionary
dictRStation[8] = nil
print(dictRStation)
print(dictRStation.isEmpty) // Bool
var dictApp:[Int:Int] = [:]
print(dictApp.isEmpty)
//iterating dictionary
print(dictRStation)
for i in dictRStation
{
print(i)
print(i.key)
print(i.value)
}