{"id":1470,"date":"2020-12-27T19:01:06","date_gmt":"2020-12-27T19:01:06","guid":{"rendered":"http:\/\/sophyonline.com\/blog\/?p=1470"},"modified":"2021-01-07T12:03:42","modified_gmt":"2021-01-07T12:03:42","slug":"keyvaluepairs-in-swift-or-how-to-create-an-ordered-dictionary","status":"publish","type":"post","link":"http:\/\/sophyonline.com\/blog\/2020\/12\/keyvaluepairs-in-swift-or-how-to-create-an-ordered-dictionary\/","title":{"rendered":"&#8220;KeyValuePairs&#8221; in Swift or how to create an ordered  dictionary"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I was facing one day with the strange \u2013at the time\u2013 task to send to a third party application an ordered collection key\/value pair from an iOS app.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I then wondered if there was a collection type, capable of doing such thing in Swift since my knowledge at the time of a dictionary in this language was pretty basic:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><span class=\"has-inline-color has-black-color\">1<\/span>.- It&#8217;s a type of hash table, hence their elements have to conform to the Hashable protocol in order to make a fast key lookup<\/p><p>2.- They cannot have duplicates keys<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then I started to search about collection types in Swift, since I haven&#8217;t used an ordered key\/value dictionary before. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First thing I learned was that using <strong>KeyValuePairs<\/strong> <strong>allows you to create a &#8220;dictionary&#8221;<\/strong> with duplicate keys and is much slower when searching for a key than a true dictionary, so you must be careful when using one of these, since the search could be much less efficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In my particular case I didn&#8217;t need to do any filter, just send exactly what I was adding, so I had no problem in that area ;D<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this image you can see that the common dictionary will print his values in different order each time. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2021\/01\/how-to-print-dic-orderer-always-1024x452.png\" alt=\"\" class=\"wp-image-1502\" width=\"508\" height=\"224\" srcset=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2021\/01\/how-to-print-dic-orderer-always-1024x452.png 1024w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2021\/01\/how-to-print-dic-orderer-always-300x133.png 300w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2021\/01\/how-to-print-dic-orderer-always-768x339.png 768w, http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2021\/01\/how-to-print-dic-orderer-always.png 1213w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Go ahead and copy the below example,  run it multiple times, you will see that the dictionary will change every time the output and print different keys each time. But, the KeyValuePair one will <strong><em>always<\/em><\/strong> print the same key\/value in the same order.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-default\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-2020-12-27-at-20.02.22.png\" alt=\"Using a KeyValuePairs to obtain a orderer key\/value pairs in a collection \" class=\"wp-image-1494\" width=\"506\" height=\"500\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>let recordTimes: KeyValuePairs&lt;String,Double&gt; =\n    &#91;\"Florence Griffith-Joyner\": 10.49,\n     \"Florence Griffith-Joyner\": 10.59,\n     \"Evelyn Ashford\": 10.76,\n     \"Evelyn Ashford\": 10.79,\n     \"Marlies Gohr\": 10.81]\n\n\nlet recordTimesNotOrdered =\n    &#91;\"Florence Griffith-Joyner\": 10.49,\n     \"Evelyn Ashford\": 10.76,\n     \"Marlies Gohr\": 10.81]\n\n\nprint(recordTimes.first as Any)\nprint(recordTimesNotOrdered.first as Any)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In other languages this type of collection is called <strong>associative arrays<\/strong>,<strong> map or symbol table<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope this post helps if needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, for more information about this kind of dictionary, you can check Apple&#8217;s doc here:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/developer.apple.com\/documentation\/swift\/keyvaluepairs\">https:\/\/developer.apple.com\/documentation\/swift\/keyvaluepairs<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was facing one day with the strange \u2013at the time\u2013 task to send to a third party application an ordered collection key\/value pair from an iOS app. I then wondered if there was a collection type, capable of doing such thing in Swift since my knowledge at the time of a dictionary in this <a class=\"read-more\" href=\"http:\/\/sophyonline.com\/blog\/2020\/12\/keyvaluepairs-in-swift-or-how-to-create-an-ordered-dictionary\/\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[3,15],"tags":[40,41,42,20],"class_list":["post-1470","post","type-post","status-publish","format-standard","hentry","category-development","category-swift","tag-keyvaluepairs","tag-orderer-dictionary","tag-programming-swift","tag-swift"],"_links":{"self":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/comments?post=1470"}],"version-history":[{"count":22,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1470\/revisions"}],"predecessor-version":[{"id":1503,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1470\/revisions\/1503"}],"wp:attachment":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/media?parent=1470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/categories?post=1470"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/tags?post=1470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}