How do we create a dictionary?
The following four lines are essentially equivalent.
All right, so I have varred my dictionary and
have created this dictionary using the dictionary type symbol.
And inside of angle brackets, I have on the first type before the comma,
I have the type that will be accepted for my keys and my key value pair.
And to the right of the comma, I have the type that will be accepted for
the values for my key value pair.
This type which is dictionary key type string, value type string,
will be applied for all of the key value pairs in my dictionary.
So I won't be able to have one key value pair that's string-string and
another key value pair that's String some other type.
They all have to be key string and
value string because of the way they have declared this dictionary.
I can initialize this dictionary using the dictionary literal
syntax of square bracket, a colon enclosed with square brackets.
So we're used to arrays.
We see square brackets and comma separated values.
With dictionaries, we'll have again square brackets and
comma separated key value pairs.
How do I know where a key value pair is?
Look for a colon, all right.
So we'll have something colon something, comma, another something, colon,
another something.
That's how you can visually distinguish between an array and a dictionary.
All right, here's my second dictionary.
Again, I say of type Dictionary, String,
String inside of angle brackets.
But notice that here I'm immediately setting the value of my
second dictionary using the assignment operator, and
then I put the type followed by open, closed parentheses which
initializes this dictionary for me with the specified key value types.
Okay, the third dictionary here, var myDictionary equals,
you got a different way, different syntax but same thing going on.
We're initializing another empty dictionary with the keys
being of type string and the values being of type string.
And then I set that equal to NOT, NOT dictionary.
And last, my fourth dictionary, I immediately set it equal to the type
followed by the open close parentheses which is, we can read as, an end.
So dictionary of type string string then end.
All right so basically, In taxes here we can use the dictionary key word followed
by the key types and value types instead of angle brackets.
Or use the dictionary little tax which is the open closed
square brackets with a colon to separate our key value pairs and
a comma separating our, Comma is separating the key value pair.
Okay, we can also initialize and populate the dictionary all in one line of code.
So here I have three planets to extend upon our solar system example.
Here we have Mercury of type [String : AnyObject?].
Now I'm going to put different information about
Mercury inside of my dictionary here.
And all of these pieces of information may not have the same data types.
That's why I'm using this generic type AnyObject.
And also I may not have a value for every piece of information,
every type of information for all of the planets in this example.
So we'll make it an optional type, any object's question mark.
We can read that in English as optional any object.
All right so now I've set what the type of the Mercury dictionary is.
Now I'm going to populate it right away.
So I say equals, open square bracket, the string name,
that's my key, from my first key value pair, colon, and
the string mercury, for the value in my first key value pair.
I also want to know in which order the planets come in our solar system.
You know the orders starting with the sign in the middle.
So, I'll create another key value pair.
The name of the second key value pair is the string order and
the value of that is 1.
So, I'll use that same syntax to create a few more planets.
We have Venus and Earth and Mars.
Notice with Earth, the type of the dictionary can be inferred.
We can remove the type and
the compiler will do its best to guess what
the type of the earth dictionary is.
Now to make this code a little bit safer for later usage in our example,
we'll just make sure that all of our planets are explicitly typed that way.
We don't have to do any kind of special do of type casting
if we come across a planet for which we didn’t explicitly type it.
All right, and I'm not going to put in all of the planets in the solar system and
all of their moons because it's a little bit too much for this example.
So we'll just pretend that we've put in the rest of the planets upto Neptune.
And for Neptune, so that we have some more data to work with,
I've listed out the 14, I think, different moons of Neptune.
All right, so for Neptune, we know some more information,
the name of it, the name of the planet, what the moons of the planet are,
and the planet's order in the solar system.
[MUSIC]