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

Please can someone help? I need help with the syntax needed to generate a random word..

import Foundation import GameKit

struct Coin { let coinFlip = ["Heads", "Tails"]

func randomToss() -> String {
    let random = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: coinFlip)
    return coinFlip[random]
}

}

The above is my code for a coin flip App. I am not sure which random generator to use for words.

Please help!

3 Answers

Hi Kavita,

If you haven't done yet - here is a solution for you:

import Foundation
import GameKit

struct Coin {
    let coinFlip = ["Heads", "Tails"]

  func randomToss() -> String {
      let random = GKRandomSource.sharedRandom().nextInt(upperBound: coinFlip.count)
      return coinFlip[random]
    }
}

I hope it helps and GL next!

Hi Nick

Thank you. I tried the upperBount Int, however this only allows me to generate numbers and not words....

Do you mean, that you tried (.nextInt(upperBound: ... ) and it didn't work?

Because it worked for me, exactly with words;

Just let me know if you need more help with it.

Hi Nick,

Thank you very much it worked!