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
Jay L
1,167 PointsHow to retrieve randomized value?
Building the data structure of the "Fun Fact App" on the Swift course, "arc4random_uniform()" is used to randomize "Facts" appeared on screen after pressing the "New Fact" button.
Based on the same logic, if I were to build a "Word of the day App" with the "facts" changed to "definitions" and the only difference is to add an extra label to show the word of the listed "definition", what are some ways to make sure the word and the randomized definition matches?
1 Answer
Jens Hagfeldt
16,548 PointsHi Jay!
Yes you can use the same logic for building a word of the day app, just as you access the facts in the factsArray in the FunFacts app you can build two similar arrays (wordArray and definitionsArray) and use the same logic for accessing their data. But instead of making two separate structs holding the words and definitions I figured that I'd make one struct holding them both.
The function for accessing a random word and its definition is therefor returning a tuple holding both the random word and the definition since they relate to each other. One note though, the randomizing function works if both of the arrays used have the same length (maybe that goes without saying). Curious as I am in how the code would look I gave it a go...
A struct holding the words and definitions can look like below
struct WordBook {
let wordArray = [
"Ant",
"Cat",
"Bass"]
let definitionsArray = [
"An ant is an insect that walk on six legs",
"A cat is a mamal that walk on four legs",
"A bass is a fish which swims with fins"]
func randomWordAndDescription() -> (word: String, description: String) {
let unsignedArrayCount = UInt32(wordArray.count)
let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
let randomNumber = Int(unsignedRandomNumber)
let randomWord = wordArray[randomNumber]
let randomDescription = definitionsArray[randomNumber]
return (randomWord, randomDescription)
}
}
In the view controller you can then access a random word + its definition like below
@IBOutlet weak var wordOfTheDayLabel = UILabel!
@IBOutlet weak var definitionOfTheDayLabel = UILabel!
let wordAndDefinitions = WordBook()
let (wordOfTheDay, definitionOfTheDay) = wordAndDefinitions.randomWordAndDescription()
wordOfTheDay
definitionOfTheDay
wordOfTheDayLabel.text = wordOfTheDay
definitionOfTheDayLabel.text = definitionOfTheDay
I hope this example helped you... Keep coding :D
/ Jens
Jay L
1,167 PointsJay L
1,167 PointsThis is indeed very helpful, especially with the code printed out. Thanks Jens!