Unit 19: Enumerations & Switch Flashcards

1
Q

Case

A

in an enum, the keyword declares a name for one of the enum’s options; conversely, in a switch, the case keyword introduces a pattern to try to match a value against

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Default

A

selected when no other option is available

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Enum

A

a keyword that declares a type made up of a group of related choices; an instance of an enum will be exactly one of the enum’s choices; the keyword “enum” originated from the term “enumeration” = listing distinct things one by one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Switch

A

a keyword that chooses between different courses of action based on some value’s characteristics

the options for the values are written in case statements

once the switch statement finds the first match between the value and a case, the block of code next to the case statement is executed

if no case statements match, the default statement’s code is executed

once one of the blocks has been executed, the programme continues running the next code after the end of the entire switch statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Multiple Choice

A

You’re often given choices to make in life. In many cases, you must pick one of a set of options, and picking nothing isn’t a choice.

For example, in the school cafeteria on a given day, the lunch options might be pasta, a burger, or soup. The cafeteria staff are in no mood for you to mix and match, so you must make a choice.

Pick one.

This system works well for the cafeteria, and for the school:

The staff know that they’ll only need to provide one of three meals.
The staff can prepare and serve each meal quickly and efficiently.
No time is wasted understanding or taking down special orders.
The school knows that everyone has had a nutritious lunch and will be energized for an afternoon of learning.

As a programmer you can benefit from applying these same kinds of controlled choices in your programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Making Decisions, Revisited

A

Consider the lunch options from the previous page. If you were writing a function to model the cafeteria, you might do this:

func cookLunch(choice: String) -> String {
    if choice == "pasta" {
        return "🍝"
    } else if choice == "burger" {
        return "🍔"
    } else {
        return "🍲"
    }
}
cookLunch(choice: "pasta")
//: - experiment: Ask for some different choices by calling `cookLunch(choice:)` a number of times. Ask for anything you can think of. What result do you get back?
/*:
 This function has the following drawbacks:
  • If you ask for anything other than exactly "pasta" or "burger" , you get soup.
  • There’s nothing telling you what you can ask for. If you can’t see the body of the function, all you know is that it takes a String, but it doesn’t tell you any of the strings it might expect.

There’s a better way to deal with situations like this.

[Previous](@previous)  |  page 2 of 21  |  [Next: Enumerations](@next)
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Enumerations

A

In Swift, you can use an enumeration to represent a group of related choices. Each choice is called a case. You can define your own enumeration types, just as you can define your own structs:

enum LunchChoice {
    case pasta
    case burger
    case soup
}
/*: 
The declaration above creates a new type, `LunchChoice`. Instances of `LunchChoice` can only be one of the three defined cases.

An enumeration is usually called by its abbreviation, enum.

The name of an enum starts with a capital letter, like all other type names.\
The name of a case starts with a lower-case letter, like the names of properties and methods.

The name of the enum should be singular, as in LunchChoice, not LunchChoices, because the value refers to only one choice, not many choices.

You make instances like this:
*/
let choice = LunchChoice.burger
//: One benefit of an enum is it limits the choices to one of its cases. You can’t order off-menu.\
//: Uncomment the line below to see the error, then comment it out again when you’re done:
//let special = LunchChoice.fish
/*:
  • experiment: Create some constants yourself for different cases in the enum. Notice how autocompletion shows you the possible options.
  • /
/*:
 Next learn about how the type system understands enums.\
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Enums and Type Inference

A

Swift can save you some typing when it expects a particular type of enum.

Here’s the LunchChoice enum from the previous page. It’s written a little differently. To save space, it includes multiple cases on a single line, separated by commas:

enum LunchChoice {
    case pasta, burger, soup
}
/*:
 On the previous page you made an enum instance like this:

let choice = LunchChoice.burger

 This variable has a type annotation:
*/
var choice: LunchChoice
//: If Swift already knows what type to expect, you can skip the enum name. Since you’ve already specified the type of `choice`, you can leave out the enum name when assigning a value:
choice = .burger
//: - experiment: Practice assigning other values to `choice` using this shorter dot notation. Notice that the autocompletion menu pops up once you type the period.
/*:
 Next, learn when it makes sense to use an enum.\
[Previous](@previous)  |  page 4 of 21  |  [Next: When to Use Enums](@next)
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When to Use Enums

A

Whenever you have a restricted group of related values in your code, it might be good to think about using an enum.

If there are no restrictions on the value, or you have a large number of possible values, enums probably aren’t a good fit.

Imagine you’re writing an app, a fun little sports game. You’re using a struct to represent each player on the field. Each player has the following properties:

name
skillLevel
team
position

name would be a String. You wouldn’t use an enum here because there are too many possibilities.

skillLevel would be an Int, since the game uses a point-style system as the user gains skill.

team would be an enum. There are only two teams on the field: .red and .blue.

position would also be an enum: .quarterback, .seeker, .pitcher, and so on, depending on how you design the game.

Define enums to represent the team and position options. Check on the previous pages for a refresher on the syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Comparing Enums

A

To make decisions using enums, you need to be able to compare one value to another.

Here’s the LunchChoice enum you’ve seen before:

enum LunchChoice {
    case pasta, burger, soup
}
/*:
 You can compare enum values using `==`, just as you have with values of the types `String` and `Int`:
*/
let myLunch = LunchChoice.burger
let yourLunch = LunchChoice.burger
if myLunch == yourLunch {
    "We're having the same for lunch!"
} else {
    "Can I try your lunch?"
}
//: - callout(Excercise): Change `myLunch` to a different choice to see a different value in the results sidebar.
//:
//: Next, make a better version of the `cookLunch` function using enums.\
//:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Enums and Functions

A

Enum values can be used as parameters or return values for functions, just like any other type.

Here’s the LunchChoice enum you’ve been working with:

enum LunchChoice {
    case pasta, burger, soup
}
//: You could rewrite the `cookLunch` function from earlier:
func cookLunch(_ choice: LunchChoice) -> String {
    if choice == .pasta {
        return "🍝"
    } else if choice == .burger {
        return "🍔"
    } else {
        return "🍲"
    }
}
cookLunch(.burger)
//: - experiment: Call the function a few more times, passing in different food choices.
/*:
 Using the `LunchChoice` enum instead of a string solves the issues that this function had when it took a `String` value. There was no way to know what was on the menu.

When calling the function, you know that you have to pass in a LunchChoice. Autocompletion will tell you exactly what the options are. You can’t pass in anything that’s not on the list, so you’ll always get what you’re looking for.

But the function could still be better.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The Problem with If

A

An if statement is useful for checking a single condition. But when if statements are used to check multiple conditions using else if, they can start to get unwieldy.

The code ends up being visually “noisy,” with a lot of repetitious text that doesn’t add new information.

The animation below shows an if statement with an enum. You can see that a lot of text is repeated and the enum cases get a little lost in the rest of the code:

The animation highlights another problem with the if statement: The last choice isn’t really anything else, it’s soup. If you were reading this code without knowing the last case in the enum, you’d have to guess.

Rewriting the function to use each specific case doesn’t help the situation much:

enum LunchChoice {
case pasta, burger, soup
}

func cookLunch(_ choice: LunchChoice) -> String {
    if choice == .pasta {
        return "🍝"
    } else if choice == .burger {
        return "🍔"
    } else if choice == .soup {
        return "🍲"
    }
    return "Erm... how did we get here?"
}
cookLunch(.soup)
/*: 
 You still need the final `return` statement. Otherwise the function causes an error because it can’t be sure you’ve covered all the possible cases in the if statements.
  • experiment: Comment out the final return statement to see an error. Uncomment it again, and try to change the value passed in to cookLunch so that the final else statement is called.\
    (Hint: How would you get an enum value that didn’t match anything in the if statement?)

Apparently if statements aren’t a great fit when dealing with enums. So what is?

[Previous](@previous)  |  page 8 of 21  |  [Next: Switch](@next)
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Switch

A

You’ve seen that if statements aren’t a great fit for checking enum values.

They add a lot of visual noise, and they can’t tell that you’ve covered all of the cases, even though the point of enums is to provide a limited list of cases.

What’s a better way to choose different courses of action based on the value of an enum?

enum LunchChoice {
    case pasta
    case burger
    case soup
}
let choice = LunchChoice.burger
/*:
 The answer is a _switch_ statement:
 */
switch choice {
    case .pasta:
        "🍝"
    case .burger:
        "🍔"
    case .soup:
        "🍲"
}
/*:
 The switch statement looks very much like the enum declaration above. That’s because they’re designed to work well together.

The switch statement starts with the keyword switch, followed by the value that it’s checking and an opening brace:\
switch choice {\
Next you add a series of cases to be checked, each with the case keyword, followed by a value and a colon:\
case .pasta:\
Since the type of the enum is known, you can use the dot syntax and leave out the type name.\
If the value being checked matches the case statement, the code between the matched case and the next case is run. Then the switch statement, just like the if statement, is done.

Next, find out some other features of switch statements.

[Previous](@previous)  |  page 9 of 21  |  [Next: Exhausting the Possibilities](@next)
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Exhausting the Possibilities

A

Switch statements have a special feature: they must be exhaustive. This means a switch statement must exhaust every possibility of the value being checked. With an enum, you can use a different case to handle every possible value.

enum LunchChoice {
    case pasta
    case burger
    case soup
}

let choice = LunchChoice.burger

switch choice {
case .pasta:
    "🍝"
case .burger:
    "🍔"
case .soup:
    "🍲"
}
/*: 
 - callout(Exercise): Add another case, `taco` to the enum. What happens to the switch statement?

You see the error Switch must be exhaustive. You’re not allowed to write a switch statement that doesn’t cover every case.

  • callout(Exercise): Fix the error by adding another case to the switch statement. Use the other cases as a guide. You can bring up the emoji picker using Control-Command-Space or copy this one: 🌮

The fact that switch statements must be exhaustive means that you can be sure that one of the cases will match the value you’re testing. This feature prevents you from accidentally missing a value. It also alerts you if you update the definition of an enum without updating any switch statements that use it.

[Previous](@previous)  |  page 10 of 21  |  [Next: The Default Case](@next)
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The Default Case

A

This enum is used to represent how good something is:

enum Quality {
case bad, poor, acceptable, good, great
}

let quality = Quality.good
//: The switch statement is a little different to the ones you’ve seen up to now:
switch quality {
case .bad:
    print("That really won't do")
case .poor:
    print("That's not good enough")
default:
    print("OK, I'll take it")
}
/*: 
 The switch statement doesn't have a case for every possible value of the enum. Instead, there is a `default` keyword which will be used if no other matches are found. This is similar to the final `else` clause when using an if statement.
  • experiment: Change the value of quality to test when the default case is used, and when specific cases are used.\
    \
    Try adding more cases to the switch statement. Notice that the default case has to be the last case in the switch statement.\
    \
    Try adding more cases to the enum.

If you add a default case to your switch statement, you won’t get an error when you add new cases to the enum. Can you think of a way this this could lead to an unexpected error?

On the next page, find out another way to match several cases.

[Previous](@previous)  |  page 11 of 21  |  [Next: Multiple Cases](@next)
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Multiple Cases

A

On the previous page you used a default case to match three of the values in this enum:

enum Quality {
case bad, poor, acceptable, good, great
}

let quality = Quality.good
/*:
 But a default case might cause you problems later on if you add new cases to the enum. The switch statement will use the default case for your new value, which may not be what you wanted. 
 Instead, you can match several values in the same case:
*/
switch quality {
case .bad:
    print("That really won't do")
case .poor:
    print("That's not good enough")
case .acceptable, .good, .great:
    print("OK, I'll take it")
}
/*:
 - callout(Exercise): Add a new case, `terrible`, to the enum. 

You would have gotten the wrong answer if you’d used a default case. By specifying each case, you’re forced to update the switch statement to deal correctly with every new case you add.

Next learn another way you can use a switch statement.

[Previous](@previous)  |  page 12 of 21  |  [Next: More Than Enums](@next)
*/
17
Q

More than Enums

A

So far, you’ve learned about enums and how to use switch statements with them. You can also use switch statements with other values.

For example, switch statements can work with strings and numbers. Since it’s impossible to have an exhaustive list of all string and number values, switch statements using these types require a default case.

let animal = “cat”

func soundFor(animal: String) -> String {
    switch animal {
        case "cat":
            return "Meow!"
        case "dog":
            return "Woof!"
        case "cow":
            return "Moo!"
        case "chicken":
            return "Cluck!"
        default:
            return "I don't know that animal!"
    }
}
soundFor(animal: animal)
/*:
- callout(Exercise): Call the `soundFor(animal:)` function a few times. Pass in known animals and unknown animals.\
\
Add a few more animal cases to the switch statement and call the function to test the new cases.
 */
/*:
Next revisit the cafeteria example using a switch statement.
[Previous](@previous)  |  page 13 of 21  |  [Next: Back to the Cafeteria](@next)
*/
18
Q

Back to the Cafeteria

A

Switch statements are very useful when writing functions that take enum arguments. Because switch statements must be exhaustive, you can be sure you’re dealing with all input possibilities. Here’s the cookLunch function you’ve been working with, rewritten using a switch statement:

enum LunchChoice {
case pasta, burger, soup
}

func cookLunch(_ choice: LunchChoice) -> String {
    switch choice {
    case .pasta:
        return "🍝"
    case .burger:
        return "🍔"
    case .soup:
        return "🍲"
    }
}
cookLunch(.burger)
/*: 
 The rewritten function is much better than the earlier version, which used strings and if statements.

With the enum, the possible input values are clear, which makes it easier for programmers to read, understand and use the function.

Inside the function, the flow of the program is obvious and there’s no need for any “just in case” code to handle unexpected inputs.

If any changes are made to the definition of the enum, the program would not be allowed to run until the switch statement is updated to match.

Find out how to add extra capabilities to your enums.

[Previous](@previous)  |  page 14 of 21  |  [Next: Enum Methods and Properties](@next)
*/
19
Q

Enum Methods and Properties

A

In the Structures lesson you saw how to define properties and methods in a struct. You can also define them in an enum. This can be very useful in providing extra behavior.

For example, you could have a property that returns a string for each value to display to the user:

enum LunchChoice {
case pasta, burger, soup

    var emoji: String {
        switch self {
        case .pasta:
            return "🍝"
        case .burger:
            return "🍔"
        case .soup:
            return "🍲"
        }
    }
}
let lunch = LunchChoice.pasta
lunch.emoji
/*: 
 The `self` keyword is used in methods and calculated properties and refers to the instance that is being asked for the property value.

You could have a method that allows you to compare two enums. For example, in a card game like Bridge, the suits are ranked like this, with the highest scoring suit on top:

  • Spades
  • Hearts
  • Diamonds
  • Clubs
 This enum represents the suits and tells you if one suit beats another:
*/
enum Suit {
    case spades, hearts, diamonds, clubs
    var rank: Int {
        switch self {
        case .spades: return 4
        case .hearts: return 3
        case .diamonds: return 2
        case .clubs: return 1
        }
    }
    func beats(_ otherSuit: Suit) -> Bool {
        return self.rank > otherSuit.rank
    }
}

let oneSuit = Suit.spades
let otherSuit = Suit.clubs
oneSuit.beats(otherSuit)
oneSuit.beats(oneSuit)

/*:
 - experiment: Add a property to the Suit enum that returns the appropriate emoji for each case: ♠️❤️♦️♣️

Next summarize what you’ve learned.

[Previous](@previous)  |  page 15 of 21  |  [Next: Wrapup](@next)
*/
20
Q

Wrap Up

A

Enumerations are used when you want to represent one of a group of related values. Each possible value is called a case.

When you create an enum, you’re making a new type. Instances of that type can only have values matching one of the specified cases.

Using enums can make your code easier to read and write, because it’s always clear what the possible values are and what they mean.

You can compare enum values with ==, or use a switch statement to test for all possible values.

Like if statements, switch statements are another way of making decisions in your code. Switch statements work very well with enums, but can be used to switch on any type of value.

Because switch statements must be exhaustive, you must handle every possible value. To handle any values that haven’t been specified, you can use a default case.

Like structs, you can add calculated properties and methods to enums.

Practice what you’ve learned with the exercises.

21
Q

Exercise: Defining Enums

A

Get some practice defining your own enums. Remember the rules about naming enums and their cases.

Define an enum for the compass directions: North, East, South, and West.

Define an enum for jigsaw puzzle pieces: corner, edge, and middle.

Define an enum for the playback modes in a music app: standard, repeat, repeat all, and shuffle.

22
Q

Exercise: Counting Chickens

A

This playground has a Chicken type built in to it. A Chicken has a breed and temper property, and both properties are enums.

Here is an array of chickens:

chickens
//:  The chickens are all hatched, so it’s safe to count them.
var chickenOfInterestCount = 0
for chicken in chickens {
    chickenOfInterestCount += 1
}
chickenOfInterestCount
//: - callout(Exercise): Update the code in the `for…in` loop to only count interesting chickens, like `.hilarious` `.leghorn`s. Check out the autocompletion popup to see what the possible values for each enum are.

//: Previous | page 18 of 21 | Next: Exercise: Replacing Bools

23
Q

Exercise: Replacing Bools

A

The following struct describes a type of enemy in a game:

struct Enemy {
    let strength: Int
    let speed: Int
    let weapon: Bool
}
/*: 
 As your game has developed, you’ve decided that your enemies might have more than one type of weapon.
  • callout(Exercise): Define an enum to represent the weapons an enemy might have: none, sword, rubberMallet and so on. Change the struct definition to use your new enum instead of a Bool.
[Previous](@previous)  |  page 19 of 21  |  [Next: Exercise: Counting Votes](@next)
*/
24
Q

Exercise: Counting Votes

A

In the Arrays and Loops playground, you had a chance to create a function that would tally votes from classmates. At the time, you could only ask yes-no questions that could be held in Boolean results.

You were worried that using strings would result in voting mistakes from typos. But now that you’ve studied enums, you can safely make your voting system a bit more sophisticated:

enum ClassTripDestination {
case beach, chocolateFactory
}

let tripDestinationVotes: [ClassTripDestination] = [.beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .chocolateFactory, .beach, .chocolateFactory, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .beach, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .beach, .beach, .beach, .chocolateFactory, .chocolateFactory, .beach, .chocolateFactory, .chocolateFactory, .chocolateFactory, .beach, .beach, .chocolateFactory, .chocolateFactory]

/*:
 - callout(Exercise):
 Without counting the votes by hand, find out whether the students prefer the chocolate factory or the beach. *Hint: Check the Arrays and Loops playground for a refresher on working with collections of data.*
 */
/*:
 ### Extension:
 In another poll, for choosing a school mascot, you decide to add an `undecided` option:
 */
enum SchoolMascotOption {
    case salamander, marmot, neither
}
import Foundation
let mascotVotes: [SchoolMascotOption] = [.neither, .marmot, .salamander, .neither, .marmot, .neither, .neither, .marmot, .neither, .salamander, .salamander, .marmot, .neither, .neither, .salamander, .neither, .neither, .marmot, .salamander, .neither, .neither, .neither, .marmot, .marmot, .neither, .neither, .marmot, .salamander, .neither, .marmot, .marmot, .marmot, .marmot, .neither, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .marmot, .neither, .salamander, .salamander, .neither, .salamander, .neither, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .marmot, .neither, .neither, .marmot, .salamander, .neither, .neither, .salamander, .salamander, .neither, .salamander, .salamander, .salamander, .salamander, .neither, .salamander, .neither, .salamander, .marmot, .salamander, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .marmot, .marmot, .marmot, .salamander, .marmot, .salamander, .marmot, .neither, .marmot, .neither, .salamander, .marmot, .marmot, .marmot, .neither, .marmot, .marmot, .salamander, .neither, .neither, .salamander, .neither, .neither, .marmot, .neither, .salamander, .salamander, .salamander, .neither, .neither, .salamander, .salamander, .salamander, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .marmot, .marmot, .neither, .neither, .salamander, .marmot, .neither, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .salamander, .marmot, .neither, .salamander, .marmot, .marmot, .salamander, .marmot, .salamander, .marmot, .salamander, .salamander, .marmot, .marmot, .neither, .marmot, .neither, .marmot, .salamander, .salamander, .salamander, .neither, .salamander, .salamander, .neither, .marmot, .neither, .marmot, .marmot, .marmot, .marmot, .neither, .marmot, .neither, .salamander, .marmot, .salamander, .neither, .salamander, .salamander, .marmot, .neither, .marmot, .neither, .salamander, .neither, .salamander, .neither, .neither, .marmot, .salamander, .neither, .marmot, .salamander, .marmot, .neither, .salamander, .neither, .neither, .salamander, .salamander, .salamander, .neither, .salamander, .neither, .marmot, .salamander, .marmot]

/*:
- callout(Exercise):
Without counting by hand, determine which option has won.

 - experiment:
 In the Arrays and Loops vote counting exercise, an extension exercise asked you to write a single function that could calculate the results of any Boolean vote. What prevents you from writing a single function for calculating both `tripDestinationVotes` and `mascotVotes`?
 */

//: Previous | page 20 of 21 | Next: Exercise: Switch

25
Q

Exercise: Switch

A

This enum represents targets that the player might hit in a game:

enum Target {
case red, green, blue, gold
}
//: This function returns a score given a particular target:
func score(target: Target) -> Int {
return 0
}
//: - callout(Exercise): Update the score(target:) function to use a switch statement and return the correct score for each target. The statements below tell you the values to aim for:
score(target: .red) // This should be 10
score(target: .green) // This should be 15
score(target: .blue) // This should be 25
score(target: .gold) // This should be 50