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
Now that we have a dictionary of the right type, let’s convert it to an inventory object so that we can use it in our vending machine.
-
0:00
The next step after converting our plist into a generic dictionary object
-
0:05
is to make an actual inventory out of it.
-
0:08
For that we're going to create another tiny class
-
0:11
which we'll place right below PListConverter.
-
0:16
We'll name this InventoryUnarchiver, like the Plist
-
0:21
converter we'll give this class is single class method.
-
0:26
We'll say static, func.
-
0:28
We're going to name this vending inventory from dictionary.
-
0:39
And it's going to have a single parameter of type String: to AnyObject,
-
0:44
which matches the return type of the plist converter method.
-
0:49
Now if you go back up and look at the protocol right over here,
-
0:55
you'll see that the type of an inventory is VendingSelection: to VendingItem.
-
1:02
So we'll set the return type of this method, down here,
-
1:05
vending inventory to output such a dictionary.
-
1:08
So we'll return vending selection to vending.
-
1:18
Admittedly we could make this more flexible right now the type of
-
1:23
an inventory as defined by the protocol is always vending selection to vending item.
-
1:29
Now a vending item is itself a protocol because remember that protocols are types
-
1:33
themselves.
-
1:35
This means that we aren't restricted to a single type we can pass in
-
1:39
as a value in an inventory object.
-
1:42
We can put in here anything that conforms to vending item.
-
1:46
The type of the keys however is vending selection which is a concrete type.
-
1:51
It's that enum that we've defined that sets the different items we can select
-
1:55
from our machine.
-
1:57
If we wanted to create a vending machine for books we've got an issue here.
-
2:01
We can't specify anything other than vending selection and so
-
2:05
far that type only encapsulates food and drink.
-
2:09
We could add our books to this enum but
-
2:11
that doesn't make sense it doesn't fit in the type.
-
2:15
For now though we'll ignore this constraint.
-
2:17
We've already got a lot of abstraction going on and
-
2:19
we'll touch on this in the future.
-
2:22
Hopefully though this still highlights the flexibility
-
2:24
of protocols to define structure in our code base without the need for
-
2:28
subclassing or any concrete types pretty early on.
-
2:33
Again this return type, is specified over here,
-
2:36
the VendingItem part of the return type is a protocol.
-
2:39
Which gives us a sort of freedom to return any type that conforms to this protocol,
-
2:44
and we don't have to subclass.
-
2:45
So inside this method we'll start out by creating an empty
-
2:49
dictionary inventory to match the final one that we want to output.
-
2:53
So var inventory, this is of type vendingSelection to vendingItem,
-
2:58
and then to this variable, we'll just assign an empty dictionary start.
-
3:04
Before we forget let's go ahead and
-
3:05
return the inventory because that's what we eventually want to do with this method.
-
3:10
Now in between these lines of code let's get it set up.
-
3:14
Our source dictionary that we're passing into the method as an argument
-
3:18
contains some values.
-
3:19
So let's iterate over the keys and values so that we can inspect and
-
3:23
work with each pair.
-
3:25
So we'll say for (key,
-
3:29
value) in dictionary.
-
3:33
And now we can iterate over the entire thing and look at each pair.
-
3:37
The keys in this dictionary are strings.
-
3:40
Eventually we want to get that to vending selection but for now that's okay.
-
3:45
The values on the other hand are any object, and
-
3:48
we can't really work with any object.
-
3:50
We need to first cast it to a more specific type.
-
3:54
If you go over to the vendinginventory.plist file and
-
3:58
inspect the contents of a particular entry you'll see that it's another
-
4:02
nested dictionary with a string for a key and double values for a number.
-
4:08
So let's cast the value from each iteration to a more appropriate type.
-
4:13
In this case, again, the value is another nested dictionary.
-
4:17
Now I made a mistake earlier these aren't both double values
-
4:21
the quantity value is an integer and the price value is a double value.
-
4:26
Now, since these are different types.
-
4:28
We have a double here, and integer here.
-
4:31
When casting this nested dictionary to a more concrete type,
-
4:35
we need to either define the type of the dictionary that contains them,
-
4:39
as having values that are of a higher type, like any object or any or
-
4:44
try to cast a more specific concrete types.
-
4:48
Any object though doesn't work here.
-
4:50
For example, this is easier to explain in code.
-
4:52
So if I say if let itemDictionary =, so
-
4:58
we want to cast a value which is of type any object to something more concrete so
-
5:02
we'll say value as.
-
5:05
And then I could do string to any object here.
-
5:10
But this doesn't work because remember any object represents a class type and
-
5:14
the different values that we're going to find in this dictionary,
-
5:17
item dictionary are either an int or.
-
5:20
So we're going to cast to any.
-
5:23
So now we have that nested dictionary that we've pulled out using a particular key.
-
5:29
We've cast that to a string to any so if we want to get price we use the price key
-
5:34
and if we want to get quantity we use the quantity key.
-
5:37
Now if you remember your dictionary basics you know that retrieving a value
-
5:41
from a dictionary using the key as we're about to do
-
5:44
always returns an optional because that key may not exist.
-
5:48
So rather than performing that logic inside this if let statement.
-
5:52
Let's add two more checks to this single if let statement to get the price and
-
5:56
quantity as well check for new values and unwrap them.
-
6:01
So first is the price so we'll say let price = and
-
6:04
we're going to get price from the item dictionary using the price key.
-
6:10
And right now the values that we get out of item dictionary are of type any so
-
6:14
we need to cast that to a more specific type.
-
6:16
So we'll say as double.
-
6:20
If that works everything is bound to price so
-
6:23
we have one more check we'll say let quantity =,
-
6:29
itemDictionary, we'll use the quantity key.
-
6:35
And this time we'll cast this to an integer.
-
6:38
If this succeeds, we have the values for price and quantity.
-
6:42
Using these values, we can now create an instance of item,
-
6:46
which is a type that conforms to VendingItem.
-
6:48
So we'll say, let item = Item and
-
6:51
we use initializer to pass through the price and the quantity.
-
6:57
Now that we have an item we're one step closer to adding a key value pair to our
-
7:01
dictionary.
-
7:03
The key is here again our P list strings right,
-
7:06
they're strings that we got out of our P list dictionary.
-
7:09
But our inventory, so
-
7:10
we want to add this item along with a key back into the inventory.
-
7:14
The problem is that we need vending selection rather than a string.
-
7:18
So we need to convert from one type to another.
-
7:21
Let's handle that in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up