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

Anthia Tillbury
Anthia Tillbury
3,388 Points

Calculating Enum Raw Values from another Enum case statement

Leaning about raw values and associated values I wanted to combine two types of Enums to illustrate how they work when type specific i.e. declaring raw values for one Enum, while relying on compiler inference for the string values.

Modifying the example from Passan when calculating American currency with raw values I wanted to instead add the dictionary of values to a switch statement linked to family members, this would be a String Enum and so I could combine an example of both type Double and type String in one small block of code, but I can't seem to make the return work:

enum Coin: Double {
    case copper = 0.01
    case pound = 1.00
    case fiver = 5.00
    case reddy = 50.00
}

enum FamilyPurses: String {
    case father
    case mother
    case firstBorn
    case secondBorn
    case newArrival

    func allowance() -> [Coin] {
        switch self {
        case .father: return  [Coin.reddy, .reddy, .reddy, .fiver]
        case .mother: return  [Coin.reddy, .fiver, .pound]
        case .firstBorn: return [Coin.pound, .copper, .copper]
        case .secondBorn: return [Coin.copper, .copper]
        case .newArrival: return [Coin.copper]
        }
    }
}

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

    for ammount in FamilyPurses.firstBorn.allowance() {
        total += ammount.rawValue
    }
    return total
}

let test = sum(having: FamilyPurses.firstBorn)

The code does not compile. I think I am having trouble at the beginning of the sum function as the video example links from an instance, while I am looking to delve into an Enum.

The code is meant to show all of what raw values are: Type specific, compiler inference at declaration and minimal use of Enum declaration.

Thanks in advance!

Jenna Dercole
Jenna Dercole
30,224 Points

Could you tell us which errors you're getting when you try to compile?

3 Answers

Shade Wilson
Shade Wilson
9,002 Points

Hi Anthia,

I am currently away from my Mac so I am unable to test my theory, but I have an idea you could try.

The problem for your code may be in your instance declaration of test:

let test = sum(having: FamilyPurses.firstBorn)

The sum function takes an argument of an array of Coins, but you aren't inputting that here. Instead, try:

let test = sum(having: FamilyPurses.firstBorn.allowance())
Anthia Tillbury
Anthia Tillbury
3,388 Points

The final line, the Instance I believe, the error complains that it cannot convert values between the two Enums, which makes sense.

From the example in the video I am led to this predicament, I am unsure how to get values from a switch statement from one Enum and compare it to values from another, over an instance method.

Anthia Tillbury
Anthia Tillbury
3,388 Points

So right you were, however the issue then became that the function was hardcoded to firstborn from the Enum, which I had to change to money to reference to the array:

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

    for ammount in money {
        total += ammount.rawValue
    }
    return total
}

So now it returns the allowance for any member of the family based on the instance.

Thanks for your help!