So will come back again in this lesson we're going to continue with the collections in Objective C and
we will be talking about a particular type of collection called a dictionary.
Dictionaries are essentially a key value pairs collection.
What it means is imagine if you have an array and then every element in the array.
So of having an index that has a numeric value it has an actual title that you can search for that title
and find that a specific item in your array.
The action is essentially the same thing that they allow you to have a key value pair or objects within
a collection.
So that's going to ex-cult next for them when I close this project I'm going to start a new one say
command line tool.
This is dictionaries project so you know dictionaries project on regular started with my very first
dictionary.
So let me get some better formatting in here as usual.
And then the first thing I'm going to do is that I'm going to say I have a..
Dictionary.
This is going to be my first dictionary that's going to be and this dictionary dictionary it up just
for me.
So I'm going to say the opposite that I have.
It's going to be my name and the key for it is going to be name G or anything like that.
So right now I have a dictionary vaire an object that is content.
This here is associated with a key called the name key.
So if I want to find out I could go ahead and say and Aslaug let's say a value for name is and whatever
object you find the later on and that value is going to be my first dictionary object for key and the
object for a key is the key I'm looking for is name me.
Let's run this or this project is going to do it is going to say let's go to my first dictionary search
for a key and that key is Name Key and find the value for it.
I'm printed in here.
Now keep in mind that the keys are arbitrary values they don't have to be anything specific if you want
call this in.
And obviously when you're searching the search for a name if you want to call it name then you know
call that one name and this one name.
I usually tend to finish them with a tidy key so I know that these are my keys.
And the other ones are my values.
Now while this might be useful it's very difficult to enter values like this.
So instead of having a dictionary with a set number of ideas they actually usually make this mutable
dictionary and be cheap.
Adding values to it.
Let's see how we're going to do that.
So now I'm going to say and it's mutable dictionary.
This is going to be my mutable dictionary that's going to be and that's mutable dictionary oarlock and
in it it is allocated on initialize.
So now there is a mutable dictionary.
It is declared it is located initialize and it can take in values such as what I would say might mean
a dictionary are a set value and the value is going to be my name for the key name key and then I'm
going to say again my mutable my mutable dictionary sets a value and the value is Vancouver and set
for a city XI.
And let's have one more my mutable dictionary and sets a value and this value is going to be I guess
on this one is going to be force Name Key.
So now I have a dictionary that has three different keys and each key obviously has a certain value.
And if I want to find any of those I could simply go ahead and say let me copy the entire thing our
name or use is for instance name of use he is under I would say my immutable dictionary object for key
on the name key.
I have to make sure that the keys I use are existing in here.
Otherwise there won't be any result.
It will then give me an error or it just wouldn't print anything.
So let's bring that out.
Purrfect that's the first one in here.
And this is the second one.
Let's print the city of the city of that is for city key.
Perfect Now as you can imagine in a larger dictionary such as this one there is 3-D Frenchy's and there
are also to different values you could actually access those and put them in an array so I could say
and Aslaug on a cheese bar and then whatever you find in my mutable dictionary all cheese.
So what is going to do is going to actually produce all of the kids put them in an array.
And then it's going to print them for me.
And the reason I know it's going to be an array is that because when I write all the keys it tells me
it's not all in this array it's going to return and it's way for me.
So all keys.
And similarly I could do the same thing for all the values I'm going to say or the values or values
or run this so what we have done here is you obviously have the two prints that you had before.
A nice thing.
All keys are our main city of course Name Key and then all the values are in Vancouver.
I was just like what we expect.
Now if for any reason you don't want to use a mutable dictionary and you want to define and declare
everything in one go you could actually do without us.
So I could go ahead and say r and s dictionary are.
Let's call this one multiply.
You use the dictionary is in this dictionary dictionary.
The last time we use a dictionary with object for key.
Now this time I'm actually going to use dictionary with objects and keys proof multiple objects and
multiple keys.
So the way you write those is like this.
I'm going to say actually with objects and keys.
Every first value every first entry will be considered a value on every second one with because a key.
So essentially right now there is one key called K for one value called the V.
Now let's try to remake this thing that we have up there.
I'm going to say it is a mere The name key and it's a good practice to actually not bring this one line
down so we can see them properly next to each other.
And then I'm going to say the next one is code word for City Name Key and then I think the last one
with the I O S on this with the horse name keep perfect.
So this is not a dictionary that the initialization Lebaran initialising you're putting up multiple
values in it.
So this is one key value.
This is another key value and this is another key value.
And just like the previous examples I could just go ahead and say print a certain thing from here say
and I like my R value for either no.
Of course he is.
And then when I say on and then I'm going to say multiple values dictionary object for key and the key
I'm looking for is horse name Keith.
That's wrong and if it all goes well it should print.
I was at the very end.
Yes value for course is I always know that because the sense of how dictionaries are built we have to
talk about something quite important that is missing dictionaries within arrays or arrays within dictionaries.
So to do that I'm actually going to comment out all of my N.S. logs so we don't see a lot of stuff here.
I'm going to go ahead and say Karmen this one out.
Carmen Carmen Carmen.
And if you're wondering how do I do this common thing is command and forward slash.
It's a hot key for commenting out anything that is in the current line.
So if we select these guys and say command forward slash it Carmens all of them out and if they do it
again it essentially on common stuff.
So now when we run our application or run successfully What we don't see anything down there because
we are not printing any values.
So in this example I want to do this I want to put two users inside one array.
So once again to use users inside one array and every user has only two entities name and city.
So just go ahead and do that.
I'm going to say N.S. dictionary use.
One dictionary is going to be an ass dictionary dictionary.
Objects and keys and the objects are these.
And you know that his name key just like what they have done before.
I have the other user which is the other piece of information which is Vancouver and that is for a city
or city she and that's one of my users.
Let's duplicate this once again.
And the copy I'm going to say use to dictionary this is going to be Steve.
Steve lives in either North San Francisco.
If that's how you spell it so good.
Two dictionaries each of them about one of my users.
And now I want to put both of them inside an array.
So I'm going to say and this mutable array Let's call this one user's array.
Is this N.S. mutable array lock and in it.
And that's not a bad time to mention this whenever you have to do this.
I look in it.
You can do only one and that is new.
So this is a lot in it I could say just do you.
Which is exactly identical to saying aloud and then saying in it.
So this too would be exactly identical.
So let's move to our users array and I'm going to say use as array as the object.
Use one dictionary.
And then I'm going to say use array and object and now add the user to dictionary.
So most of my dictionaries are now added inside my array and if I print my array and log whatever object
you find in the user's array.
If you print this we should be able to see the entire thing in one big array.
So just like Will we expect that this is the array.
So that is the array that you Panthers these are the two round brackets.
Within that there is one object column or the other object.
And as we expected within that curly brackets which is our dictionary there is the city of Vancouver
named Amir.
And then for the other use the city key sanfrancisco name key Steve.
Now in the next few lessons when we talk about flu we will learn how to search between all of these
elements and find exactly what we want.
But for now I just want to show you something real quick.
Imagine we wanted to go and find our second array object in the array and from that we wanted to get
the name of the user.
So I'm going to say name of their second are our dictionary.
Does the name here I would say on one line down I would say use Array object that index 1 that is the
second one.
Remember arrays start from 0 0 1.
So now we got that first dictionary.
Then I'm going to open another bracket and then close up and say object for CI and they keep I'm looking
for is name key.
So it's essentially saying if you wanted to see it visually is saying go to the user's array in the
users or go to the object that index one or not to this one but this one in here says one object for
the key name key and that's the name key.
Are printed.
So let's see what we get.
Run that I sing name off the second dictionary is Steve or a dad naming name for the second use I should
have said so I can use a name for this I can use that is Steve.
Just like how you expected it to be.
So that's about that.
Dictionaries are of value key pair collections.
You can have so many different keys and so many different values inside the dictionary to find a certain
value within a certain dictionary.
We look for object for key.
We get that we can also get things such as all the keys and we can get all the values there is more
of these methods such as getting all the values for a certain key or getting all the kids that correspond
to this value.
What are those things where can see them as we go along and we actually you know use dictionaries inside
our applications.
For now just learn how to essentially initialize dictionaries either with a single key value pair or
with a mutable matter that you can keep adding new values to them or with multiple values in one go.
Obviously about nesting in different dictionaries inside an array which is a very common practice.
And of course finding values inside them getting the array from the array.
Getting that particular object that is a dictionary.
And from that dictionary getting the object for a particular cheat would be essentially what you would
need to do.
So that's about that.
Let's keep it up to here on in the next lesson.
We'll move on.
I treasons on for loops on control froths.