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 Creating a Data Model Finishing Up Our Model

Benjamin Prehn
Benjamin Prehn
3,534 Points

How can i make sure that the fact dosen't show up two times?

I'm working on a very simple app similar to this one, but it's important that i don't get same Question two times because it's ruining the experience of the app..

I can't figure out how to do it myself, so i hope some of you guys had an idea :-)

Benjamin Prehn
Benjamin Prehn
3,534 Points

thanks for the answer.

Is it something similar to this? could a for loop do the job?

// For loop

var todo : [String] = ["Retur calls","Wrie blog","Cook dinner","Pickup Laundry","buy bulbs"]

var index = 0 // Index while index < todo.count { // condition println(todo[index]) index++ // increment }

for var i = 0; i < todo.count; i++ { println(todo[i]) }

3 Answers

your best bet is to keep a record of which facts have been displayed, then check this list/array before displaying the next fact, then if it has already appeared loop and get a different one

(Pseudo-code)

get fact identifier

if fact identifier is in used fact array get new fact

J Kim
J Kim
15,218 Points

You could make a copy of the array and delete the item that is shown.

When no more items are left in the copied array return its values to the original.

Lauren Hibbs
Lauren Hibbs
1,872 Points

I actually did this for my app... First I changed it from a stunt to a class. Then I created a second array of the same length as the fact array of boolean values. Each time a fact is accessed from the factList array, the corresponding value in the boolean array is set to true.

My randomFact method returns an optional, if there are no facts remaining it returns a nil.

class FactBook {

    let factsArray : [String]

    var hasBeenShown : [Bool]

    init () {
        factsArray = [
            "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."
        ]
        hasBeenShown = [Bool](count : factsArray.count , repeatedValue : false)
    }


    func randomFact() -> String? {

        let randomNumber = Int (arc4random_uniform(UInt32(factsArray.count)))
        var ctr = 0

        //checks if there are any unshown facts after this fact
        while (ctr+randomNumber)<factsArray.count{
            if hasBeenShown[randomNumber+ctr ]{
                ctr++
                continue
            }
            else{
                hasBeenShown[randomNumber+ctr]=true
                return factsArray[randomNumber+ctr]
            }
        }

        ctr=0

        //checks if there are any unshown facts before this fact
        while (randomNumber-ctr)>=0{
            if hasBeenShown[randomNumber-ctr]{
                ctr++
                continue
            }
            else{
                hasBeenShown[randomNumber-ctr]=true
                return factsArray[randomNumber-ctr]
            }
        }

        return nil
    }
}