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

Deysha Rivera
Deysha Rivera
5,088 Points

for coin in coins

Why is coin.rawvalue used and not Coin.rawvalue? How does coin have any values assigned to it?

enum Coin: Double {
    case penny = 0.01
    case nickel = 0.05
    case dime = 0.1
    case quarter = 0.25
}

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

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

    for coin in coins {
      total += coin.rawValue
        }
    return total
}

sum(having: coins)

1 Answer

Magnus Hållberg
Magnus Hållberg
17,232 Points

The coin variable is just a placeholder name for the individual values of the array you input to the function. You could call this anything. Coin with a upper case “C” refers to the type Coin, the enum you created. Hope this helps