In this video, go through the chapter challenge step-by-step.
(soft music) - [Instructor] Alright, let's start off our little Merchant Shop with an array. Var shopitemsarray and I'm going to give it some values Stone Shield, Bronze Hammer and Gold Helm. I'm also going to declare a shopitemsdictionary.
And for this I'm going to use keys that are the same as our arrays, so Stone Shield, and I'm going to use the values as its price. So I'm going to say this is equal to 15 gold. You can get a Bronze Hammer for 25 and a Gold Helm for 35 why not. We're actually going to add in one more, called Diamond Longsword.
This is going to be a little expensive, we'll set this at 1000. Now I'm using Diamond Longsword as an extra value in the shopitemsdictionnary because I want to check if our shopitemsarray contains it, and if not I'm going to insert it, so it gives us a chance to use some of the array functions. So with shopitemsarray, I'm going to check if it already contains a Diamond Longsword and this is going to return false because it doesn't.
So let's add it. Shopitemsarray, insert, and here I'm going to use the second option again, inserts a new element into the collection. The new element is going to be Diamond Longsword. And I'm going to put this at position two. Now if we use quicklook, we'll see that we have Stone Shield, Bronze Hammer, Diamond Longsword and Gold Helm.
Alright now let's pull out an item and store it in a variable called selecteditem. We're going to use shopitemsarray, we're going to use the subscript syntax, and I'm going to pull out the Diamond Longsword. So I'm going to use index two. Now here's where things get interesting. I'm going to use the selected item here, to get the price out of the shopitemsdictionnary. So var selecteditemprice equals shopitemsdictionary, subscript, and I'm just going to pass it in selecteditem, because this is a string.
Alright so that's pretty cool. Now we've got our selecteditemprice, which is a thousand for our selected item which is our Diamond Longsword. So that's all working. Now let's do some work with sets. We're going to create a new set called fullarmorset and here I'm going to have this equal to Diamond Helm, Diamond Armor, Diamond Greaves, and the last item, Diamond Bracers.
I'm also going to create another set called currentarmorset and in here I'm only going to have Diamond Helm and Diamond Bracers. So I'm short of the full armor set. Now let's use one of the set methods to figure out which items I still need to complete my armor set. Fullarmorset and I'm going to use subtract, other sequence, I'm going to subtract my currentarmorset from that.
If we use quicklook, it's going to say yes, that we need Diamond Armor and Diamond Greaves to complete our set, so that's great. Last, let's create a tuple, called armorpiecetuple, and we're going to initialize this with some named values. Name is Diamond Bracers, cost is going to be equal to 55 gold, and canequip is going to be true.
Now for the final task, just access the canequip with .notation from armorpiecetuple. Alright so this all looks great, let's head onto the next section.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs