Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Enumerations and Optionals in Swift Objects and Optionals Enumerations With Raw Values

Cameron Hyatt
Cameron Hyatt
5,948 Points

Coins for-in loop question

When copied the For In loop for the sum function, Xcode is giving some errors. When I type out

case .penny: total += 0.01

It gives me an "Enum case 'penny' not found in '[Coin]'" error but when I change .penny to Coin.penny, it works fine! It does this for the other cases in the loop, so I'm getting an error I don't see Pasan getting. Does anyone have any idea why?

Edit: Xcode actually still gives me the error.

let coins: [Coin] = [.penny, .nickel, .dime, .dime, .quarter, .quarter, .quarter]

func sum(having coins: [Coin]) -> Double {
    var total: Double = 0

    for coin in coins {
        switch coins {
        case Coin.penny: total += 0.01
        case Coin.nickel: total += 0.05
        case Coin.dime: total += 0.10
        case Coin.quarter: total += 0.25
        }
    }

    return total
}

1 Answer

Dan Lindsay
Dan Lindsay
39,611 Points

Hey Cameron,

It looks like in your switch statement, you are switching on coins, when you should be switching on coin.

So if you change the code to look like this:

for coin in coins {
        switch coin {
        case .penny: total += 0.01
        case .nickel: total += 0.05
        case .dime: total += 0.10
        case .quarter: total += 0.25
        }
    }

it should all work properly. Hope this helps!

Dan