Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Dictionaries are a fundamental construct in most computer languages and Objective-C is no exception. We'll examine how dictionaries are structured as well as how we create them using the literal syntax.
-
0:00
One very common construct in programming is the dictionary.
-
0:03
In Objective-C., we use two classes of dictionaries.
-
0:06
NSDictionary and NSMutableDictionary.
-
0:10
If you don't know what mutable means, it simply means changeable.
-
0:13
So you can probably guess that instances of NSMutableDictionary
-
0:17
can be changed in more ways than just a regular dictionary.
-
0:20
In fact, it's best to use NSDictionary only for
-
0:23
static dictionaries that you don't really ever intend on changing.
-
0:27
In about three minutes, we'll wheel out the old Xcode and give those two a try.
-
0:31
But before that, let's talk about dictionaries in general.
-
0:35
So as I mentioned, dictionaries can be found in lots of computer languages and
-
0:38
for obvious reason, they're a natural reflection of how we think about and
-
0:42
categorize data in the real world.
-
0:44
I mean that sort of makes sense.
-
0:46
Way before we ever had one of these on our desktop, we all had one of these.
-
0:52
Now a paper dictionary is set up in key-value pairs.
-
0:55
The Key is the word you're defining and the Value is the definition or
-
0:59
list of definitions.
-
1:01
If we look at the key dog.
-
1:03
We see a value of, I don't know, a furry animal far superior to the cat.
-
1:08
That key value pair is also called an entry.
-
1:12
Our paper dictionary has hundreds of pages and defines tens of thousands of words,
-
1:16
but it only has the word dog defined in one place.
-
1:19
That key only appears once.
-
1:22
Yes, it might have several definitions but
-
1:24
those are all collectively, the value of that one key.
-
1:29
In a paper dictionary, we're confined to just using strings of letters and words.
-
1:33
But in the realm of programming, dictionaries are much more versatile.
-
1:37
The keys are strings but the values can be all sorts of objects.
-
1:42
Now it's important to note that objects mean objects.
-
1:45
You can't store primitive values like ints or floats in an NSDictionary.
-
1:49
Instead you have to convert them into a numerical object first,
-
1:53
like an NSInteger or an NSNumber.
-
1:55
That isn't hard by the way, but it does often trip people up.
-
1:59
Let's say we wanna store data about runners on our track team.
-
2:03
We wanna record their first name, last name, weight, gender,
-
2:07
fastest 100m dash and also what other sports they like to play.
-
2:11
We could do that for a theoretical runner really easily like this.
-
2:15
Here we see, we've used a mix of strings, numbers, and
-
2:18
perhaps something like an enum for gender.
-
2:21
We even have a collection being used to hold the list of other sports.
-
2:24
Now remember that collection is still just one value tied to the key "other sports".
-
2:30
The value just happens to be a collection.
-
2:33
You can see how this type of construct would be useful
-
2:36
since we could create a similar dictionary for every member of the team.
-
2:39
Once we do we,
-
2:40
we have a spot perfectly designed to hold the information we know we'll want.
-
2:44
Similarly when a runner breaks their own record,
-
2:47
we have a spot to enter the new time which will automatically replace the old time.
-
2:52
In thinking about our track team as a whole,
-
2:54
the dictionary comes in handy again.
-
2:56
When it's time to rank all the runners by speed, we could just
-
2:59
loop through all the dictionaries focusing on the 100m best values.
-
3:04
Or if we wanted a list of just male runners, we could again get through and
-
3:08
filter based on the gender value.
-
3:10
In both cases, we're keeping a lot of data neatly organized for
-
3:13
each runner, but we can selectively act on or ignore that data as we like.
-
3:18
Saving us lots of potential confusion and processor time.
-
3:22
Now you folks all know me from Treehouse, so
-
3:24
you probably think I spend all my time thinking about computers and code but
-
3:28
in actuality I spend most of my time thinking and talking about food.
-
3:32
With that in mind, let's create a new command line tool.
-
3:38
And we'll call it Burger Barn because hey,
-
3:40
who doesn't wanna eat their burgers in a barn, right?
-
3:44
Now remember, since we're just focusing on learning the language of Objective-C.
-
3:49
We use a command line tool,
-
3:51
that way we don't have to worry about any user interface.
-
3:54
Once you start building real apps,
-
3:56
you'll build much of your app graphically, not simply in code.
-
4:00
Okay, so Burger Barn is a very simple restaurant, so
-
4:03
simple in fact that they only sell two things, burgers and shakes.
-
4:07
And yes those burgers come with fries, so
-
4:09
don't worry about us cheating our make believe patrons.
-
4:12
Now the owner of the barn, named Barney,
-
4:14
by total coincidence, wants to start tracking the orders in a computer system.
-
4:19
Up until now, everything just gets written on a notepad.
-
4:21
That works for the cooks and the waitstaff, but
-
4:23
management is left with a stack of papers rather than any actionable data.
-
4:28
Barney, the owner, gave us a list of things he wants to know about each order.
-
4:32
I'll open up our main, and then we can paste
-
4:35
that information that we wanna keep track of down here In a comment.
-
4:43
We wanna know the number of burgers, the number of shakes, the number of customers.
-
4:48
If the order was for take out or not and then the order subtotal.
-
4:52
Seems like we could use a dictionary to store that stuff, right?
-
4:56
To do that, let's give ourselves some room here in the main function.
-
5:04
Then begin with the class we want.
-
5:07
That's gonna be NSDictionary.
-
5:12
Then *orderDict, which is what we'll call this instance of the NSDictionary class.
-
5:20
After that we can simply say = and
-
5:23
begin typing in the key value pairs we wanna store.
-
5:27
I'll type it all in and then we'll review it.
-
5:30
Okay, the first thing to know is that we're assigning keys and
-
5:34
values to this dictionary using literal syntax.
-
5:37
That's what all these @ symbols are about.
-
5:39
Our NSDictionary begins with an @{ which tells the compiler,
-
5:45
hey what's coming next is a dictionary.
-
5:47
Then inside our dictionary, we create our keys which are strings using @ and
-
5:53
quotes just like any NSString.
-
5:56
Our values on the other hand, are a mix.
-
5:58
Some are NS numbers and Boules.
-
6:01
And for those, we just use an @ symbol before the value we want
-
6:04
as that will convert it to an NS number object.
-
6:07
By the way, I've included a handy guide to literal syntax in the teacher's notes.
-
6:11
It's worth checking out.
-
6:13
So now we have a dictionary representing an order of five burgers,
-
6:19
three shakes for a table of four customers that is not take out and
-
6:25
the subtotal is still $0.00.
-
6:28
I left the subtotal as 0 because we'll be computing it ourselves in a minute.
-
6:32
But before we go there, let's drop in a breakpoint, run our app and
-
6:36
see what we get.
-
6:41
All right, just as we expected, we have a dictionary with five key value pairs and
-
6:45
they're all the values that we want.
-
6:50
Now Barney told us that the burgers cost $4 and shakes cost $3.
-
6:54
Let's see if we can extract the values from our dictionary and use them to
-
6:58
compute the subtotal, then insert that value back into the orderDict we created.
-
7:04
Now we use magic numbers in our sample dictionary.
-
7:07
But what variables will we want to help create and build our logic?
-
7:11
Take a minute, jot down the variables you'll wanna create,
-
7:14
and then continue to the next video where we'll put it all into action.
You need to sign up for Treehouse in order to download course files.
Sign up