際際滷

際際滷Share a Scribd company logo
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.
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.
DICTIONARIES
 Syntax:
var Identifer: [KeyType:ValueType]
 Example:
var myDictionary: [String:Int]
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(:).
DICTIONARY LITERAL
 Syntax:
var Identifer: [KeyType:ValueType] = [key:value,
key:value, ...]
 Example:
var dictionary: [String:Int] =[ "one" : 1,
"two" : 2,
"three" : 3 ]
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] = [:]
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
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")
ACCESSING DICTIONARY
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
print(info["first_name"]) OUTPUT
Raj
Use of nil in Dictionary
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
info["first_name] = nil
for item in info
{
print(item)
}
OUTPUT
("job_title", "Singer")
("last_name", "Sharma")
PROPERTIES OF DICTIONARY
 count
 isEmpty
 keys
 values
ACCESSING DICTIONARY KEYS
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
for i in info.keys
{
print(i)
}
OUTPUT
first_name
last_name
job_title
OUTPUT
last_name
job_title
first_name
OUTPUT
job_title
last_name
first_name
ACCESSING DICTIONARY VALUES
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
for i in info.values
{
print(i)
}
OUTPUT
Raj
Sharma
Singer
OUTPUT
Singer
Sharma
Raj
OUTPUT
Sharma
Raj
Singer
UPDATE PAIR/ITEM INTO DICTIONARY
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
info[last_name"] = "Kaur"
info.updateValue("Jaya",forKey:"first_name")
ADDING PAIR/ITEM INTO DICTIONARY
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
info["middle_name"] = "Vig"
print(info.count)
OUTPUT
4
REMOVE PAIR/ITEM FROM DICTIONARY
var info: [String: String] = [
"first_name" : "Raj",
"last_name" : "Sharma",
"job_title" : "Singer"
]
info.removeValue(forKey: "job_title")
print(info.count)
OUTPUT
2
METHODS OF DICTIONARIES
 removeValue(forkey:key)
 removeAll
 updateValue(value,forkey:key)
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
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
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

More Related Content

Dictionaries IN SWIFT

  • 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.
  • 3. DICTIONARIES Syntax: var Identifer: [KeyType:ValueType] Example: var myDictionary: [String:Int]
  • 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(:).
  • 5. DICTIONARY LITERAL Syntax: var Identifer: [KeyType:ValueType] = [key:value, key:value, ...] Example: var dictionary: [String:Int] =[ "one" : 1, "two" : 2, "three" : 3 ]
  • 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")
  • 9. ACCESSING DICTIONARY var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] print(info["first_name"]) OUTPUT Raj
  • 10. Use of nil in Dictionary var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] info["first_name] = nil for item in info { print(item) } OUTPUT ("job_title", "Singer") ("last_name", "Sharma")
  • 11. PROPERTIES OF DICTIONARY count isEmpty keys values
  • 12. ACCESSING DICTIONARY KEYS var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] for i in info.keys { print(i) } OUTPUT first_name last_name job_title OUTPUT last_name job_title first_name OUTPUT job_title last_name first_name
  • 13. ACCESSING DICTIONARY VALUES var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] for i in info.values { print(i) } OUTPUT Raj Sharma Singer OUTPUT Singer Sharma Raj OUTPUT Sharma Raj Singer
  • 14. UPDATE PAIR/ITEM INTO DICTIONARY var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] info[last_name"] = "Kaur" info.updateValue("Jaya",forKey:"first_name")
  • 15. ADDING PAIR/ITEM INTO DICTIONARY var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] info["middle_name"] = "Vig" print(info.count) OUTPUT 4
  • 16. REMOVE PAIR/ITEM FROM DICTIONARY var info: [String: String] = [ "first_name" : "Raj", "last_name" : "Sharma", "job_title" : "Singer" ] info.removeValue(forKey: "job_title") print(info.count) OUTPUT 2
  • 17. METHODS OF DICTIONARIES removeValue(forkey:key) removeAll updateValue(value,forkey:key)
  • 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