Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
Dictionary in Swift is used to store elements. It is 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.
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.
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
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.
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.
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.
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.
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)” )
2) In this example we are also 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 also 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.
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)” )
Also 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.
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.