Swift 4.2 Essentials

Dictionaries

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

The third type of Collections in Swift are called dictionaries. In the video, we will explore how to create and manipulate these types in your code lines.

Keywords

  • Swift 4
  • Ios
  • Tutorial
  • sets
  • arrays
  • dictionaries

About this video

Author(s)
Mark Hoath
First online
17 November 2018
DOI
https://doi.org/10.1007/978-1-4842-4232-2_9
Online ISBN
978-1-4842-4232-2
Publisher
Apress
Copyright information
© Mark Hoath 2019

Video Transcript

Okay, dictionaries, also known as key value pairs. So, for every key there is a corresponding value, and dictionaries used extensively now ever since the advent of really windows programming, and then the HTML, and the web. Dictionaries have become an increasingly large part of programming. So, it’s important to understand how they work. So, for every item in the dictionary there is the key, and there is the value. They don’t need to be of the same type. So, they’ve got this generic form. Dictionary key comma value, direct form of key directory of any data types, but once they’re defined you’re gonna maintain the data type that you defined them as. So, to create an empty dictionary we simply go variable names of integers equals, and again we use the array symbol, all those braces, but we have two items separated by a semi colon. Not a stein, a string. So, again enter the key and a string as the value, and then we put a little initialize that on the end to give us out our empty dictionary. While we can do this, we can go names of integers, and then we can go to our key of 16, and set the value to the string of 16. So, this is not creating an array. There’s not 16 elements in the array. There’s only one element in our dictionary. It’s just that the key is 16, and the value is the string of 16. So, it can get a little bit confusing. If you want to empty your dictionary then you just provide the braces with the colon in the middle because it’s a dictionary. So, set the key to zero, and the value zero on the left and right of that colon.

So, let’s have a look at creating a dictionary with literals by annotation. So, airports is what we’ll be looking at, and just to make it different it’ll be a string and a string. So, we got a string for the key, and a string for the value, and I think we will just create one, and also pretty close to San Francisco. So, there we have it. We have an airport dictionary, and we have the one item with key of SFO, and a value of San Francisco. It works, pretty much the methods are all very similar to arrays if not identical. Airports dot count will tell us how many items that we have in the dictionary, and of course that would be one. There is the airports dot is empty. There’s nothing in this dictionary, which returns true or false, and you can add items to the dictionary. So, we can go airports arrays Sid equals Sidney. So, we have a second airport added to our dictionary, and we can also let old equals airports dot remove value for key. So, if we wanna get rid of San Francisco then we can get this new value old which would be the dictionary for San Francisco, and airports will have had San Francisco removed. And so, Sidney would be the only one that is left. So, we can print old, and we can also print airports, and just like we did for sets and arrays you can iterate through all the values. However, because there’s keys end values you can iterate through either. So, you can go for code in airports dot keys, and that will just give us the key values as if old and Sid. Just forget about the remove values right now, or we can go for name in airports dot values, and again we can print it out by code or name.

If, for some reason, we just want to get them into separate variables then we could do it as a topple. So, we can go for code name in airports. Then we go print code, and print name, and so, that’s the use of topples for you. Now dictionaries are unordered. So, if you want to get an ordered list or alphabetical list of these codes or names you would need to take those names, and put them into an array. So, we go bar codes equals an array of string, and then rather than passing just the empty races we will pass airports dot keys, and if we wanted the names we can just use a similar theme. Caps lock, airports dot values, and that would put them all into place, and then we would go let sorted codes equal codes dot sorted, and that would fix it all up.