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

Alexandre Krakani
Alexandre Krakani
1,569 Points

Previous Crush after the first String

import GameKit

class FactModel { // See, this is a class now. let facts = [ "Test1", "test2", "Test3", "test4", "Test5", "test6", "test7", "Test8", "test9", "test10", "Test11", "test12", "test13", "Test14", "test15", "test16", "Test17", "test18" ] var index = 0 // This is the property that will allow us to track what fact we are up to.

func getRandomFact() -> String {  // No change to this method.
    let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)

    return facts[randomNumber]
}

func getNextFact() -> String {
    let fact = facts[index]  // We grab the fact before we increment the index.
    if index < facts.count - 1 {  // We make sure we did not give the last fact in the list.
        index += 1  // We increment the index so the next fact is ready to go.
    } else {
        index = 0  // We wrap around to the first fact because we just gave the last one.
    }

    return fact
}

func getPreviousFact() -> String {
    let fact = facts[index]
    if index < facts.count - 1{
        index -= 1
    } else {
        index = 0
    }
    return fact

}

}

let myFact = FactModel()