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

How do I do this?

I finished Enumerations and Optionals. However, before moving onto the next course, I decided to take the rest of the week reviewing the content and putting it to practice. Because honestly, a lot of it went over my head. In the email congratulating me for my accomplishment, It suggested going back to my FunFacts app and seeing what I can replace with Enumerations... so I did just that. Problem is, I can't get it to work! I am currently trying to replace my FactModel type with an Enum instead of a struct or class. I'm stuck. The only error it currently returns is this: " Enum case 'Fact' is not a member of type [String] "

Heres my code:

import GameplayKit

enum FactModel {
    case Fact(String)


    func getRandomFact() -> String {

        let facts = [
            "Ants stretch when they wake up in the morning.",
            "Ostriches can run faster than horses.",
            "Olympic gold medals are actually made mostly of silver.",
            "You are born with 300 bones; by the time you are an adult you will have 206.",
            "It takes about 8 minutes for light from the Sun to reach Earth.",
            "Some bamboo plants can grow almost a meter in just one day.",
            "The state of Florida is bigger than England.",
            "Some penguins can leap 2-3 meters out of the water.",
            "On average, it takes 66 days to form a new habit.",
            "Mammoths still walked the Earth when the Great Pyramid was being built." ]

        let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)
        switch facts {
        case FactModel.Fact:
            return facts[randomNumber]
        }


    }


}

1 Answer

Hi Ryan, There are few modifications needed to get it to work .. 1) you don't need associated value in "case Fact(String)" as you are not using it, so just make it "case Fact" 2) in the switch statement, you need to switch on "self" instead of "facts", so it should read "switch self ". This will clear the error you get.

I hope it works with you. good luck. Safwat

Thanks! I had forgotten about switch self... :)

Its says it cannot be constructed because it has no valid initializers?

Never mind, it worked, just had to add a default value for the Fact(String) :)