Either in this list then you're going to learn how to use dictionaries in swift.
It's somehow very similar to what you have in Objective C especially from a concept perspective.
It's exactly the same thing.
It's just that the syntax is a little bit different.
So let's go to X code in here and I'm going to start a new playground.
I'm going to call this Rawn let's call it my dictionaries in the swift.
So let's see how we got to write our first dictionary.
So the first thing is I need a variable.
I'm going to call it dictionary and then dictionary and what I call it to be of the type dictionary
with values that are extreme and a string.
This is explicitly telling dictionary what kind of values we're going to have in it.
And then I could just go ahead and say dictionary are initialized.
But if you want to be far more explicit which doesn't really make any difference.
You could say explicitly or type I notated of the type the string and its thing so that's like a much
longer the most the longest possible way of writing a dictionary.
What if I wanted to write a lot easier I could just go ahead and say.
My dictionary is.
And then within brackets I would say for name key column the value is name and then column or for city
key column.
The value is then who were so on and so forth.
So she value paired items.
So right these key value pair paired items.
Not every hair item is so simple as that I can just build my dictionary in either of the vase.
I can go ahead and change values in them like I would say dictionary value for a something called either
you.
IDG unique identification make that this random you know set of characters if they you ID exist is kind
of updated.
If it doesn't exist essentially added I could go to my dictionary and say my dictionary value for name
key make it to be or not.
I mean and if I go ahead and get access to this value now you will see that when we execute our play
playground we should see I don't mean here.
Perfect.
So that's exactly you know what we expected to see.
Now the only thing that is left in here is what you play when I go through the items of my dictionary
how could I go through every item in my dictionary to do that we need to learn a new concept something
that is completely new in swift and it doesn't exist in Objective-C and that concept we call it a tool.
So to pros help us do that.
And before I actually show you how to work let me show you how they work on a dictionary.
And then from there on we go to using it for other purposes.
So let me take all of these out and just keep the things that I know I need.
So that's that.
And then I don't need a dictionary.
I don't need that action and I don't want to update my dictionary.
I only want to have this dictionary.
So in my dictionary that's called my dictionary.
I want to do this.
I want to say four and then I'm going to say J and B.
In my dictionary do this say trains she is interpretation of Che as the key.
And then I'm going to copy this pasted on save value is that of course we now let's go ahead and run
this.
Well if I was to execute my playground we will see that it says she a name key value is a key is the
key value is Vancouver.
And if you have hundreds of items in our dictionary the same thing would happen if we had an array of
dictionaries.
It would have been the same thing you would go through the items of the array for each of them we would
run this thing and would print every value if you wanted to find out if any of these values is equivalent
to either know a certain name be withdrawn and if statement in the middle of this.
Now that's all good and well what we don't know is this particular structure.
How is this working.
So that is what we call it Tupilak.
So let's see how a tuple work is essentially a way for containing several values in all their insight
only one invaluable.
So I could in theory I could say larrup let's say.
My name is going to be a Panthers of Amir and Jake for instance.
So that's essentially a but is it is a tuple of two variables.
Both of them are or there.
This is zero.
This is one and they are sitting inside the variable called my name.
So if I go in here and say print and in the printer I say my name is zero.
That's going to print on here I'm guessing.
Let's see that.
So yes I was going to print that and then I could of course go ahead and use Avorn or I could do this
I could go ahead and actually name them I could say first name is here and then last name is J.
And now I could not only access them by their index I could not live sustained by their names I could
say print my name by that.
So let's see how that works.
And again I get the same thing.
So tuples are a very good way for combining multiple values that for whatever reason they have to be
together put them all inside the one variable but give them names.
Tuples are kind of like.
They can more concise version of struct.
So you can kind of think of them as astroid if you already know what they saw.
So in a programming language if not don't worry about it.
Once you get to a structure we will learn about those.
But you put it on much like like a concise and a brief version of a strike that they do things for us
by giving them names we could potentially you know put this as to be multiple values for different reasons
so they could be there are user first name last name and then I would say you ID that unique ID is going
to be somebody else.
And I would say their email is going to be and thats going to be an email address essentially.
So on and so forth.
And now if I wanted to access this guy I could simply go ahead and say user not email.
I would print for me this particular you know value that it has in its email.
In this case I mean are the examples that come not this is the same format we are following in a dictionary.
We are saying give me a tuple of two values let's call them key and.
Right.
I'm going to say a tuple that is called Keyon Val inside my dictionary because I know every object in
here is of that nature is a key and Yvonne.
And now I could go ahead and say Gee and of course Saval obviously is going to work the way we expect.
So that's about that.
That's how we make dictionaries in swift that's how we introduce them as literature.
If you want we could explicitly tell the compiler that they are dictionaries of the type whatever it
is they are trying to do.
You might sometimes in other people's code see it like this that dictionary and then they do something
like this they write a string on a string.
And that simply means again they are trying to note that they are trying the they are doing they are
introducing a dictionary they are declaring a dictionary of the type of string and a string that has
crunchie no values but it is initialized.
This is very similar to saying that my array is empty.
So it is initializing an array.
There's just nothing.
It's the same thing here the dictionary here is saying this is the type.
And there is nothing in it.
Of course the values could be anything that you want to imagine it could be a string an integer that
could be a string and any that could be a string and a string depending on your situation and your case
most likely in a dictionary the keys are a string simply because that's just what makes sense right.
So with that in mind I think we're good.
Let's move on to our next lesson.