In this video, walk through the chapter challenge step-by-step.
- [Instructor] All right, let's dive right in. For task number one I'm going to create a type alias called Attack and I'm going to name tuple values, so name of type String and damage of type Int. And for task number two I'm going to create a type alias called ItemClosure and for a parameter it's going to have a dictionary of type String for keys and Integer for values and it's going to return Void.
Now for task number three I'm going to declare a new function called attackEnemy, this is going to take in a damage parameter of type Int and it's not going to return anything. Here I'm just going to print Enemy hit for damage with a new line break. And for number four I'm going to create an overloaded version of attackEnemy, so func attackEnemy.
This time it's going to take in an attack parameter of type attack and it's going to return a Boolean value. So here I'm just going to print out you attacked with, I'm going to use our new attack type, so attack.name, give it a new line. Enemy hit for attack.damage and then a new line once again.
On here, you should have a lowercase O. Now I can't forget to return a Boolean so here I'm going to return false and that should clear up the error. All right, so far so good. For task number five I'm just going to use the overloaded methods. So attackEnemy for damage, I'd say for 23. Now for the overloaded method I'm going to create a new variable called enemyWasDefeated to capture the Boolean return and set this equal to attackEnemy that takes in a type attack.
Here I'm going to call this Power Slam and it's going to be 88 damage. Let's take a look at our debug log just to make sure we're doin' everything right. Yep, enemy hit for 23 damage. You attacked with Power Slam, enemy hit for 88. All right, so let's use our type alias closure here then declare a new function, and I'm going to check what this needs to be named. fetchShopItems.
It's going to take in a closure and this is going to be of type ItemClosure and we're not going to return anything for this one. I'm going to have a local variable here called shopItems and this is going to be a dictionary and I'm going to fill this out real quick. Let's see. Mage Robes for 34 gold, Rangers Tunic for 13 and Warriors Belt, why not, for five.
I'm going to use my closure parameter and feed it shopItems. Perfect. All right now let's call fetchShopItems and I'm going to call the dictionary we're going to get itemsDictionary. And in our body of code I'm just going to iterate over the dictionary. So we already know how to do this. For item and value in itemsDictionary we're going to print out item worth value.
And there we go. Debug log shows rangers tunic is worth 13, mages robe is worth 34, and warriors belt is worth a whopping five gold.
- 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