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

Array, dictionary what do I do here?

So I'm trying out my xCode and Swift skills after the How To Make A Simple Swift 2.0 app.

So I needed to do something that used the buttons to change the strings that the labels show. So I wanted to make a would you rather app. It's working fine but there is a problem.

When You start the app and it shows the questions they don't really match. Since I am using arrays and taking a random value from it. I just simply get

SomethingRandom or SomethingRandom.

So say my arrays looked like this:

let wouldYou = ["Take a european sightseeing vacation", "Never eat chocolate again" ]
let orWouldYou = ["Take a 1 week trip to Hawaii", "Legally change your name to H****r"]

This may not be the best way to do it. And I'm sure there is something I could of done better but I am trying to figure this out. So my functions then look like this

    func getRandomQuestion() -> String {
        let randomQuestion = GKRandomSource.sharedRandom().nextIntWithUpperBound(wouldYou.count)
        return wouldYou[randomQuestion]
    }
    func getRandomOr() -> String {
        let randomQuestion = GKRandomSource.sharedRandom().nextIntWithUpperBound(orWouldYou.count)
        return orWouldYou[randomQuestion]
    }

So as you can se it will obviously just pick a random one from the two arrays and show it. Remember that I have two labels with the questions and two buttons with the choices that you choose.

So to solve this I was thinking right. How would you take the randomise the "arrays number" like [1] for the wouldYou array and then use the number(can't remember what they are called) it has to pull out the same number of the other array orWouldYou.

This is kinda hard to explain so I'm sorry if it confuses you :/.

How do I do this?

1 Answer

Hi Nils,

I'm assuming that you want to pair your two questions? Is that correct? If so, you could have the separate arrays, and use the same index value for each array. If you did this, you would want the random number generator to be scoped to each function but to have a shared scope. You could do this multiple ways. One would be to create a function that generates the random number and then a second function that grabs the string from each array.

However, I would suggest, if my assumption is right about you wanting to use paired "would you" questions, is just create an array of arrays. That way as you add questions to your app it will be easier to manage the pairs and you just grab both strings when you grab the randomly selected array.

Here is what I cobbled together quickly:

import GameKit

let questions = [
    ["Travel to Hawaii", "Travel to Fiji"],
    ["Fly to the moon", "Fly to Saturn"],
    ["Eat chicken", "Eat steak"]
]

func getRandomQuestion() -> [String] {
    let randomIndex = GKRandomSource.sharedRandom().nextIntWithUpperBound(wouldYou.count)
    return questions[randomIndex]
}

let pairOfQuestions = getRandomQuestion()
pairOfQuestions[0]
pairOfQuestions[1]

let anotherPairOfQuestions = getRandomQuestion()
anotherPairOfQuestions[0]
anotherPairOfQuestions[1]

Thank you so much!!!