SlideShare a Scribd company logo
1 of 20
Download to read offline
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

Similar to Dictionaries IN SWIFT

11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptx
MrNikhilMohanShinde
 
NSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to SwiftNSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to Swift
Andreas Blick
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov
 

Similar to Dictionaries IN SWIFT (20)

Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
 
Lists in Python
Lists in PythonLists in Python
Lists in Python
 
PYTHON PROGRAMMING (2).pptx
PYTHON PROGRAMMING (2).pptxPYTHON PROGRAMMING (2).pptx
PYTHON PROGRAMMING (2).pptx
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
DICTIONARIES (1).pptx
DICTIONARIES (1).pptxDICTIONARIES (1).pptx
DICTIONARIES (1).pptx
 
Java
JavaJava
Java
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptx
 
Dictionaries
DictionariesDictionaries
Dictionaries
 
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
MongoDB.local Berlin: How to add your favorite language to MongoDB CompassMongoDB.local Berlin: How to add your favorite language to MongoDB Compass
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
 
Datatypes in Python.pdf
Datatypes in Python.pdfDatatypes in Python.pdf
Datatypes in Python.pdf
 
NSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to SwiftNSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to Swift
 
Databases - Unit 2.pdf
Databases - Unit 2.pdfDatabases - Unit 2.pdf
Databases - Unit 2.pdf
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 
1-Object and Data Structures.pptx
1-Object and Data Structures.pptx1-Object and Data Structures.pptx
1-Object and Data Structures.pptx
 
enum_namespace.ppt
enum_namespace.pptenum_namespace.ppt
enum_namespace.ppt
 
introduction to python
 introduction to python introduction to python
introduction to python
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 

More from LOVELY PROFESSIONAL UNIVERSITY

More from LOVELY PROFESSIONAL UNIVERSITY (19)

Enumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFTEnumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFT
 
Control structures IN SWIFT
Control structures IN SWIFTControl structures IN SWIFT
Control structures IN SWIFT
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties IN SWIFT
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
Soft skills. pptx
 
JAVA
JAVAJAVA
JAVA
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Unit 1
Unit 1Unit 1
Unit 1
 
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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