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 Build a Simple iPhone App with Swift 2.0 Structs as Data Models Finishing Up Our Model

Help needed to understand the getRandomFact method

struct FactModel {
    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."]

    func getRandomFact() -> String {
        let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)
        return facts[randomNumber]
    }
}

I'm having a hard time understanding what is happening here. First of all; why is the return type of the function a string? Shouldn't it be like... an array? We are returning the facts array, right? Then; I'm not even going to go deeper in to the whole GKRandomSource -nightmare over there, but the "return facts[randomNumber] is just not clicking with me. Maybe another example of a similar situation would help?

Thaanks!

2 Answers

Hi there,

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."]

Think of the facts array like a container that contains the Fact-Strings.

let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)

The getRandomFact() Method first generates a random number which is smaller than the number of Strings in the array (because array indexes start at zero).

return facts[randomNumber]

Then, from the array, it returns the Fact-String at the index of the randomly generated number.

Hope I could help! If there's anything you don't understand, don't hesitate to ask me again :)

Best Regards,

Philip

I stepped away from the computer for a while and came back to this step by step answer and I'm so grateful. Thank You! Makes sense now :)

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Simple really once you under stand it. with this code:

let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)

you are randomly getting a number in a range from 0 to the upper bound of the array index in facts array. In other words, I have 0 through the highest number in the array index, I close my eyes and grab one, that is my randomly selected number. I then find the string that matches the index number and put it in the randomNumber variable box. I then return the variable box that has one string form the array. If you did return the array you would get an error because you can only assign a string, not an array, to a labels text property.

Hope this makes sense!

THANK YOU! :) You cleared up the method for me!