Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Stored values order is completely changed in Dictionary

I tried to display datas which is in Dictionary format. Below, three attempts are there. First attempt, output order is completely changed. Second attempt, output order is same as input. But, in third attempt, I declared variable as NSDictionary. Exact output I received. Why this changes in Dictionary? Kindly guide me. I searched for Swift's Dictionary tag. But I couldn't found out.

//First Attempt var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"]         println(dict)  //output: [name2: Roy, name1: Loy]  //Second Attempt var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"]         println(dict)  //output: [name2: Loy, name1: Roy] -----------------------------------------------------------  //Third Attempt With NSDictionary var dict : NSDictionary = ["name1" : "Loy", "name2" : "Roy"]             println(dict)  //output: {     name1 = Loy;     name2 = Roy; } 

ANOTHER QUERY: I have used play ground to verify. My screen shot is below:

play_ground_screen_shot

Here, In NSDictionary, I gave name5 as first, but in right side name2 is displaying, then, in println, it is displaying in ascending order. Why this is happening??

play_ground_screen_shot_2

Here, In Dictionary, I gave name5 as first, but in right side name2 is displaying, then, in println, it is displaying, how it is taken on the Dictionary line. Why this is happening??

like image 221
McDonal_11 Avatar asked Apr 13 '15 08:04

McDonal_11


People also ask

Is Swift dictionary ordered?

There is no order. Dictionaries in Swift are an unordered collection type. The order in which the values will be returned cannot be determined. If you need an ordered collection of values, I recommend using an array.

Is dictionary unordered in Swift?

Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you can't insert a value of the wrong type into a collection by mistake.

How dictionary works internally Swift?

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.

Are JSON Dicts ordered?

Dictionary doesn't have any order. @Nirav D, ordering is useful when JSON data is serialized and deserialized into a dictionary. Since the dictionary has no order, then you might end up writing a lot of different JSON strings even when coming from the same dict.


2 Answers

This is because of the definition of Dictionaries:

Dictionary

A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering.

There is no order, they might come out differently than they were put in.
This is comparable to NSSet.


Edit:

NSDictionary

Dictionaries Collect Key-Value Pairs. Rather than simply maintaining an ordered or unordered collection of objects, an NSDictionary stores objects against given keys, which can then be used for retrieval.

There is also no order, however there is sorting on print for debugging purposes.

like image 119
Schemetrical Avatar answered Sep 20 '22 19:09

Schemetrical


You can't sort a dictionary but you can sort its keys and loop through them as follow:

let myDictionary = ["name1" : "Loy", "name2" : "Roy", "name3" : "Tim", "name4" : "Steve"]   // ["name1": "Loy", "name2": "Roy", "name3": "Tim", "name4": "Steve"]   let sorted = myDictionary.sorted {$0.key < $1.key}  // or {$0.value < $1.value} to sort using the dictionary values print(sorted) // "[(key: "name1", value: "Loy"), (key: "name2", value: "Roy"), (key: "name3", value: "Tim"), (key: "name4", value: "Steve")]\n" for element in sorted {     print("Key = \(element.key) Value = \(element.value)" ) } 
like image 21
Leo Dabus Avatar answered Sep 17 '22 19:09

Leo Dabus