Hey everybody.
Welcome back to the voting party.
Mark Price here at death's slopes of duck farm.
And today we're going to talk about dictionaries and what I'd like you to do before we start is actually
do me a favor and do a google search type in how do hash tables work in computer science how to hash
tables work in computer science and go read some articles and start getting an idea of what a hash table
is and then pauses video and go do that and come back here like in 15 minutes because I want you to
have a base foundation.
Did you do that.
I'm sure you didn't because you listened to me until you start getting those job interviews and then
they're like how does this work.
You're like Mark you're right this whole time I should have been listening to what I just said and read
about this and this and that and that.
But instead I just was hoping that you had a video on it and whoa.
But anyway I went to read it and I got the job.
As always happens.
Don't listen to me.
Listen to me.
Whatever.
Do whatever you want your life.
All right.
So creating a new playground here and we're going to call this one dictionary.
Dictionaries.
So in Swift's hash table it's called the dictionary and in different languages the hash tables are hash
maps or in this case dictionary here.
And I'm going to go ahead and store this in my dictionaries folder.
OK.
There's a playground and I'm a pull up in this nice little info graphic that Apple has created.
So there are three major collection types in swift.
We have the array K which is N which is an ordered set.
Excuse me an ordered list of items and you can have multiple values of the same type in an array.
So for instance index 0 can have let's say the word six eggs but also index 5 could have the exact same
word.
Six makes doesn't matter.
OK a linear list ordered items.
OK.
Now we've got something called a set.
Now a set is has items that should only have one type of value meaning the values need to be unique
but they're not ordered.
OK but they are unique.
And then we've got this cool thing here called the dictionary.
Now you're going to use dictionaries and arrays more than your sets.
When you're working with swift The dictionary has keys and values.
And we're going to have an example similar to this today basically where the key is of a certain type.
And then there is a value that's related to that key so that the values can all be the values can be
the same as the other values of the other keys but the keys must always be unique.
OK.
And this is not ordered.
OK.
And there are actually very complex and efficient algebra algorithms working under the hood that help
you retrieve and fetch items.
Imagine if you had a dictionary that had a million keys in it.
You know it's not going to go through the entire list of keys.
It's going to start breaking things down and segregating out sections until it finds the key and the
fastest route.
Very complex things going underneath the hood.
Again if you do that search I told you too on how to hash tables work.
There's some amazing YouTube videos on how this actually works underneath the hood if you're into that
kind of thing.
And anyway so we're going to be working with the dictionary today and we're going to work with keys
and values.
So this is straight out of the Swift books with three e-book.
Very important that you read that book.
Of course I've said that before.
And here we go.
We're going to create our first dictionary names of this and I want I want you to do names of integers.
Let's say we want a dictionary that lets us give a word a visual word that you can print out for a number
type meaning like let's say I want the number three to say the word Three things like that.
So what I can do is I can say names of integers equal or subscript here the square brace here and we're
going to say three equals three.
Now I know what you're thinking.
Oh this is just like an array.
This is like the third index right.
No it is not.
This is actually the name of the key I just made the names of the keys of type int.
OK this has nothing to do actually with an array it's not a subscript in that.
You're grabbing an ordered amount of items.
OK.
This is just happened to be what I the type I chose for the key.
This could also have been a string here which is interesting.
So this is a simple example of your very first dictionary.
And what we're doing here is we're just creating one on the fly.
Initializing it using type inference.
OK.
We're creating a dictionary with a key of type and and a value of type string.
I would not be able to do something like this and a three in here.
OK.
Because the value needs to be a string can assign value of type and destroying cast of keys and values.
All of this may look familiar to you if you've worked with the race names of integers or see one more
than let's say 44 equals forty four OK.
The idea behind this dictionary is there's a unique key and then there's a value a word value for that
we're just explaining in words so simple example.
If you want to click quickly clear out a dictionary.
Empty all the items out of it.
It's as easy as doing this.
OK square braces colon square braces it's going to clean out all of your keys and all of your values.
Nice and you always do a more real world.
Example let's say we have an app.
OK.
And let's talk about airports of the world we want to have an app that has all the airports of the world
well every airport can only have one unique airport code.
Imagine if airports had the same airport codes it would be crazy like imagine if Ireland had an LAX
Los Angeles had an L.A. X you know and Dubai had an L.A. X wouldn't work that would mess things up so
luckily we have unique airport codes that we can use throughout the world and each one has its own name
its own unique name.
And you know what.
Maybe the airport codes are different and maybe two airports can have the exact same name but that wouldn't
matter because it has its unique code and that's the most important thing.
But we still need the name.
So that's the advantage of using dictionaries is the Keys have to be unique.
They have to be listed they're not secret or dictionary.
Some of our airports.
And this is going to be of type string so we're going to do it in explicitly declared dictionary.
So the key is of type string and the value is of type string.
Now let's go ahead and initialiser here right off the bat.
So we're going to create some dictionary literals here that we're initialising right when we declare
as well.
So Toronto
can't.
So we've just created a dictionary that comes preloaded with one element in it or one key value combination.
But we want to do more so we're going to put a comma here and then elex.
OK.
And what we're going to do is say loss or Los Angeles.
Pretty cool actually.
Los Angeles say international.
OK.
So and I actually commoner's not go there.
There we go.
OK.
So what is happening here.
So we put in the key name first and then a colon.
OK.
The colon separates the keeping the values of the key name goes first the call and then the value the
comma separates the elements or the combinations.
So this is one key value combination.
This is a second and it's separated by a comma.
So key name colon value comma key named colon value comma key name call and value comma.
And we didn't have to explicitly declare it like so we could have used type inference and it should
just work out of the box for us indicating that yes this is a string key and this is a string value.
Pretty cool.
So now we've got our dictionary full of airports and there's some of the other things we can do with
this.
We can print the Air Force dictionary has it lets you some string interpellation airports that count
airport or Osei elements items.
OK let's make this a little bigger here so you can see it's area.
Put the quotation to soon.
So the airport's dictionary has two items.
Is that true.
Yes it is one and two guys so you can get the count of a of a dictionary.
And what else can we do if our ports is empty.
You can see if it's empty or not.
And we can print the airport's dictionary is empty.
OK.
So you can check if it's empty.
The simple little command and you're probably wondering well how do I add new items to a dictionary.
Well since we're not making an ordered array where an element goes onto the back of the list we're inserting
at a certain place we don't care about that.
All we care about is the key.
So we can just say airports let's add a new one.
LHR equals London.
So what we're doing here is we're setting LHR.
This is the key name.
OK.
We're set in that unique key key value to be of the value of the type and type but the value to be London.
OK.
So the unique key LHR is going to be equal to London.
Okay.
And we just added a new element at this point in time.
Okay well what if we want to update this or override it.
It's just as simple as doing the exact same thing.
Okay.
So what we did here was whatever was in that spot.
We've now overwritten.
It's gone.
So that's how you replace elements.
That's also up to elements did it matter if one of these key names existed before or not.
No it does not matter because it will be created the moment you assign it there.
OK so if you're thinking well there's nothing in there and I tried to assign some to it it's going to
give me an error.
That's not the case.
Not unlike in an array where if you try and grab an item that doesn't exist.
OK.
You get the crash you know it's different here.
OK.
What else let's say airport.
Loads up here.
All right say airports credit Nouwen dev equals slopes International.
Didn't you guys know that Dev slopes has its own airport now and we have Wi-Fi and roller coasters and
birthday cakes.
It's really cool.
And then we realized it was a dream and it doesn't exist so how do we actually remove an item completely
we want to go gone key and everything because there is no there's Loeb's airport with cakes and Wi-Fi
and roller coasters would be cool.
We probably had a draft in there too.
But my dreams are now crushed so Everts dev Beagle's Neal.
You can set it to nil which will completely destroy and remove the item.
It is gone.
So I hope you're starting to see oh I can work with keys and values they're unique.
I can do different things with them.
You're going to use these a lot.
Hey we're just scratching the surface here.
You can also iterate through through and a dictionary which is cool as well.
You can get all the keys and values out of it which we'll do right now.
So Kalsu they're here for each airport code an airport name in airports.
OK.
So what we're doing here is a for loop.
OK.
And the for loop in this case in a dictionary it's giving us what's called a tuple.
OK a tuple is a data type or a data construct that has one or more elements in it.
OK so it's returning as a tuple you don't have to remember that just know how to use this.
OK.
And what it's going to do is it's going to pass in the key name and the value.
I could also have said key and Val.
OK.
Same exact thing.
So it's going to give us the key name and the value and it's going to go through the entire dictionary
on each one of those.
And there is an algorithm that goes underneath the hood that's efficient and grabbing this information
though not as efficient as other ways of accessing information.
In most cases one in many cases you don't want to iterate through an entire list of dictionaries if
you want to iterate through a list of something you know maybe an array is better data structure but
you can definitely go through here and I have before so say airport code.
All right.
And then we'll put up the last call in here and then we're going to say airport name.
OK.
So we're just going to go through and print the code in the name or the key and the value.
And here it is.
There's the key there is the value there's the key there is the value key and value.
OK.
Looking good.
Very good.
All right.
So there's our airport to airport name perfect gap.
What else can we do.
We can also just do the keys and values individually so each airport code or we can even just say for
each key in air port Stuckey's.
So you can print up the key is right
when you pass in the key
pretty cool and we can also do the same thing with the BIOS for each vowel.
An airport style value is now valid just a variable that I've created a constant
you
organise mass and bell here OK so value value value.
So you can work with kids and values.
The kids are unique.
You're mostly at most of the time you're going to work with strings here Njord in your data types you
can play around with other things.
But one of the big uses of dictionaries is when you're working with Jaison data that you're getting
from the Internet it's going to come down in a bundle Jae's on file and you're going to grab things
by keys and values because Jaison is very much similar to the notion of dictionary keys and values.
And if you don't know what I'm talking about just take a look at it right here.
You can go to a Star Wars API that's this free API that was created on the internet that has lots of
information on it.
And so right here it's pulling up some space on this one.
Luke Skywalker.
And so this is a dictionary where the Vout are the keys are of type string.
So here's a key called name of type string.
So name this string Luke Skywalker the value type of this one is a string.
Let's find one that's not a string here.
These are all strings on here.
Let's look at some of their other API features to show you how it's going to kind of translate in your
code.
Israel strings as well.
They could also been numbers.
This this company or this person chose to put all the values in as strings instead of numbers so that's
really important to to understand is when you download information from the internet or Jason you have
to abide by whatever they have created for you.
So whereas I may have put this as a number an integer they chose to use a string for both the key and
the value.
There's another API that I like to work with as well.
OK mine are active pokey API.
OK another one that somebody put up which is kind of cool.
And here's one perfect example so.
So this is a dictionary object right.
And the keys are all strings.
But in this case the key is.
So the key ID has a value that's a number it's an integer and base experience is an integer.
Height is an integer is default is a boolean.
So you can have a boolean value type.
So this is this is interesting to look at this one on this one.
The key name is a string but the value is an array.
That's really interesting.
Can we do that with swift.
Like let's say we wanted to create nobilities.
That's really interesting.
Let's give it a shot and say bar let's say abilities.
Me a dictionary right.
So the dictionary or the bill t's dictionary is going to be a dictionary of type string.
And then what we're going to do is we're going to say an array and the array is going to be an array
of what can be an array of dictionaries right.
So let's see what it does here.
Of type string and string.
I'm just curious.
The syntax is wrong here.
But anyway my point here is you can have.
You can have different items inside of your dictionaries.
OK.
Such as a boolean and in OK you can do all kinds of different things with your dictionaries.
It doesn't have to just be strings and integers.
So lots of cool stuff.
This is just scratching the surface here.
OK.
Just touching the surface if you want to get a better synopsis of how dictionaries work again do that
Google search I told you about how to hash tables work in computer science you can also look at Jason
to start getting a feel.
When I said Jaison That's J S.
Oh and get a feel of how things work with with web and how it might translate to Swift.
And that's it.
Lots of good information.
So get all in and let's move on and forward.
Mark Price does slopes dotcom at CNN.