1. DICTIONARIES
• A dictionary is an unordered collection that
stores multiple values of the same type.
• Each value from the dictionary is associated
with a unique key.
• All the keys of one dictionary must have the
same type.
• All the values of one dictionary must have the
same type.
2. DICTIONARIES
• The type of a dictionary is determined by the
type of the keys and the type of the values.
• For example a dictionary of type[String:Int] has
keys of type String and values of type Int.
• Unlike arrays, items in dictionary do not have a
specified ordered.
• Each key of one dictionary must be unique.
4. DICTIONARIES
• You can initialize a dictionary with a dictionary
literal.
• A dictionary literal is a list of key-value pairs,
separated by commas, surrounded by a pair of
square brackets.
• A key-value pair is a combination of a key and
a value separate by a colon(:).
6. EMPTY DICTIONARIES
• Similar to arrays there can be empty
dictionaries in Swift.
• You can create empty dictionary using the
empty dictionary literal ([:]).
• Example:
var myEmptyDictionary: [String:Int] = [:]
7. CREATING & ITERATING DICTIONARY
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
for (key, value) in info
{
print("(key): (value)")
}
OUTPUT
job_title: Singer
last_name: Sharma
first_name: Raj
8. CREATING & ITERATING DICTIONARY
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
for item in info
{
print(item)
}
OUTPUT
("job_title", "Singer")
("last_name", "Sharma")
("first_name", "Raj")
18. COMPARING DICTIONARIES
var d1 = ["name": "Tim", "surname": "Cook"]
var d2 = ["name": "Kate", "surname": "Perry"]
// Check if they are the same
if d1==d2 {
print("D1 is same as D2")
}
else {
print("D1 is not same as D2")
}
OUTPUT
D1 is not same as D2
19. COMPARING DICTIONARIES
var d1 = ["name": "Tim", "surname": "Cook"]
var d2 = ["name": “Tim", "surname": “Cook"]
// Check if they are the same
if d1==d2 {
print("D1 is same as D2")
}
else {
print("D1 is not same as D2")
}
OUTPUT
D1 is same as D2
20. COMPARING DICTIONARIES
var d1 = ["name": "Tim", "surname": "Cook"]
var d2 = ["name": “TiM", "surname": “Cook"]
// Check if they are the same
if d1==d2 {
print("D1 is same as D2")
}
else {
print("D1 is not same as D2")
}
OUTPUT
D1 is not same as D2