EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Swift Dictionary

Home » Software Development » Software Development Tutorials » Swift Tutorial » Swift Dictionary

Swift Dictionary

Definition of Swift Dictionary

dictionary in Swift is used to store elements. Dictionary also contains one key while storing elements to it, later on, we can use these keys to access the elements store in the dictionary. Dictionary in swift does not maintain the insertion order of the element in which they are stored, they are unordered in nature. Also, they store the elements of the same type as value. In the coming section, we will discuss more their internal working in detail for better understanding. We can perform adding, deletion, filtering, modification, and many more operations using dictionary methods in swift which are very easy to sue and handle by the developer.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As discussed that dictionaries are used to store the elements, but the insertion order will not be maintained by them. Let’s see how we can create a dictionary in swift to use them while doing programming for better understanding see below;

var variable_name = [Key_Type: Value_Type]()

As you can see in the above lines of syntax we are creating a blank dictionary in swift. First, we have to give a name to the variable. Followed by the key type and variable type. We will discuss more these two things in detail in the coming section of our tutorial. For now, let’s see one practice syntax to get a better idea see below;

var mydemo = [String: String]()

The above lines of code have given you a better understanding of the dictionary in swift. In the next section, we will see how they work internally in swift by taking some examples.

How Dictionary works in Swift?

As now we already know that dictionary in swift is used to store the elements and perform some action when needed. But they store element of the same type this prevent us from any runtime error in the future. Also, they do not maintain the insertion order of the elements stored. while creating the dictionary object in swift we also have to pass the key for an element, by the use of it we can access the element later on. In this section, we will first discuss its working and later we will see one sample example for beginners to get the idea of how we can use this while programming in swift.

Internal working: Dictionary in swift takes two arguments while creating its object. These two arguments are ‘key’ and ‘value’. dictionary in swift works in the same like map work in java.

Popular Course in this category
Swift Training (10 Courses, 11+ Projects)10 Online Courses | 11 Hands-on Projects | 63+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,109 ratings)
Course Price

View Course

Related Courses
Windows Forms Training (4 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)

1) Key: In is used to access the value of the element stored in the dictionary. This has to be unique for each element present in the dictionary. Key can be an Integer and String. In terms of swift, these key is often referred to as ‘identifier’ of the element.

2) Value: This can be anything, also they can be duplicated because we access them by using the key we assign. While creating the value we give the type of the value as well, so they should be of same type throughout the programming for a particular object of dictionary in swift.

3) Fetch element from the dictionary: To access any element from the dictionary object we can use their key because they are unique. Below see the syntax to access them;

e.g. :

var variable_name = y_dictionary[key]

We can use ‘[]’ brackets to pass the key and get the result element as the output based on the key.

4) delete element from the dictionary: We can also delete element from the dictionary, for this, we have one method available in a dictionary named ‘removeValue’. Let’s see its syntax for better understanding see below;

e.g. :

Live Demo
var variable_name:[Key:Value] = [] var result_variable = variable_name.removeValue(forKey: your_key_here)

In the above lines of code, we are trying to delete elements from a dictionary based on the key. We are using ‘removeValue’ method from a dictionary. Inside this, we are passing our key to be deleted. After deletion, its value would assign to ‘nil’ in swift.

5) Modify the dictionary element: We can also do modification on the element for this we can use ‘updateValue(forKey:)’ method from dictionary. We can pass the new value of the element inside this method only. For better understanding see the syntax below;

e.g. :

var variable_name:[Key:Value] = [] var resule_variable = someDict.updateValue("pass new value here , forKey: your_key)

In the above lines of code, we are trying to modify the value of a particular element using the key. Inside the ‘updateValue’ method we are passing two params as the input one is ‘new value’ and another one is ‘key’ of the element.

Examples

1) In this example we are trying to create the dictionary object after that we are assigning it various elements, Followed by assessing the elements using their ‘key’. This is a simple example for beginners to use while programming.

Example #1

var mydemo:[String:String] = ["key 1":"value 1", "key 2":"value 2", "key 3":"value 3", "key 4":"value 4", "key 5":"value 5", "key 6":"value 6", ] print("Dictionary cerated successfully !!!")
var result1 = mydemo["key 1"] var result2 = mydemo["key 2"] var result3 = mydemo["key 3"] var result4 = mydemo["key 4"] var result5 = mydemo["key 5"] print( "Value of first result is= 1 is \(result1)" )
print( "Value of second result is= 2 is \(result2)" )
print( "Value of third result is= three 1 is \(result3)" )
print( "Value of fourth result is= four is \(result4)" )
print( "Value of fifth result is= five is \(result5)" )

Output:

Swif1 Dictionary 1

2) In this example we are trying to delete the value from the dictionary object for this we are using ‘removeValue’ method available in a dictionary object. Inside this method, we have pass the key that we want to delete and it will make its value as nil in result. This is a sample example for beginners.

Example #2

var mydemo:[String:String] = ["key 1":"value 1", "key 2":"value 2", "key 3":"value 3", "key 4":"value 4", "key 5":"value 5", "key 6":"value 6", ] print("Dictionary cerated successfully !!!")
var result1 = mydemo.removeValue(forKey: "key 1")
var result2 = mydemo.removeValue(forKey: "key 2")
var result3 = mydemo.removeValue(forKey: "key 3")
var result4 = mydemo.removeValue(forKey: "key 4")
var result5 = mydemo.removeValue(forKey: "key 5")
print( "Value of first result is= 1 is \(result1)" )
print( "Value of second result is= 2 is \(result2)" )
print( "Value of third result is= three 1 is \(result3)" )
print( "Value of fourth result is= four is \(result4)" )
print( "Value of fifth result is= five is \(result5)" )

Output:

Swift Dictionary 2

Conclusion

By using a dictionary we can store elements into it. To fetch them we have keys available inside them. Also, we can perform many operations like deletion, modify the element, filtering the element, accessing of the element, and so on. They provide us safety from any runtime error which can come up by casting of the element.

Recommended Articles

This is a guide to Swift Dictionary. Here we discuss the definition of Swift Dictionary, How dictionary works in Swift, and Examples with code implementation?. You can also go through our other suggested articles to learn more –

  1. Swift For Loop
  2. Swift Version
  3. What is Swift?
  4. Swift Operators

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Swift Tutorial
  • Basics
    • How To Install Swift?
    • Swift Version
    • Swift Operators
    • Swift For Loop
    • Swift Dictionary
    • Swift forms
    • Swift map
    • Swift Interview Questions

Related Courses

Swift Training Course

Windows Forms Training Course

Kali Linux Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Swift Training Course Learn More