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

How can I get Next string instead of Random?

import GameKit

struct FactModel { let facts = [ "Test1", "test2", "Test3", "test4", "Test5", "test6", "test7", "Test8", "test9", "test10", "Test11", "test12", "test13", "Test14", "test15", "test16", "Test17", "test18" ]

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

return facts[randomNumber]

}

}

3 Answers

Nathan Tallack
Nathan Tallack
22,159 Points

You could add another method that is getNextFact. But you will need your object to keep track of what fact it is up to, and that gets a little tricky because structs dont like you mutating their properties from within their funcitons, which is an advanced topic for another time.

So, first we change your struct to a class (to get around that nasty method mutating problem) and you end up with something that looks like this.

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
    }
}

So you could work with this object like this.

let myFact = FactModel()
print(myFact.getRandomFact())  // This prints a random fact but does not increment the index.
print(myFact.getNextFact())  // This prints the first fact (index 0) in the list.
print(myFact.getNextFact())  // This prints the second fact (index 1) in the list.

And so on... :)

Alexandre Krakani
Alexandre Krakani
1,569 Points

I modified the code to get a previous fact. But every time it reaches the last one the app exit out . Any suggestion? ////////////To Get previous fact //////// func getPreviousFact() -> String { let fact = facts[index] if index < facts.count - 1 { index -= 1 } else { index = 0 } return fact }

Nathan Tallack
Nathan Tallack
22,159 Points

Yeah, you are going to need to wrap around the other way.

So, you will need something like this.

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

        return fact
    }

Pretty much the same, but we are going backwards until we hit 0 then we will wrap around to the last fact in the array again. You could work it like this.

let myFact = FactModel()
print(myFact.getRandomFact())  // This prints a random fact but does not increment the index.
print(myFact.getPreviousFact())  // This prints the first fact (index 0) in the list.
print(myFact.getPreviousFact())  // This prints the last fact (index 17) in the list.
print(myFact.getPreviousFact())  // This prints the next to last fact (index 16) int he list.

Swift is awesome right! :)

Alexandre Krakani
Alexandre Krakani
1,569 Points

Thanks a lot. I never knew this was possible. if index == 0 { index = facts.count - 1

Nathan Tallack
Nathan Tallack
22,159 Points

Yeah, one of the great things about class objects is you can change their property values using their own methods. Makes your objects very powerful.

Have fun with Swift!!! :)