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 trialStanley Gao
3,889 PointsHow to link two arrays in a struct together?
I'm trying to modify the Fun Fact app a little bit and I run into some problem.
In the new app, I want to wrote two arrays in the struct, one facts and one descriptions. For each fact there is a specific description assigned to it.
However, the problem comes. I can still randomize from the fact arrays but what about the description.
I have some conceptual idea but I'm not sure how to code them, for instance:
Randomize number first, with a given number I can print out two arrays with the same number.
Maybe I can do something before the method? Like assigning a description to each fact?
1 Answer
kirkbyo
15,791 PointsHey Stanley,
I was having the same problem with a random quotes app that I just submitted to the app store. I decided to use a plist and store all my quotes with there authors name inside the new plist file. Here is the code I used
var word: String = ""
var definition: String = ""
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// Generates a random number
func randomNumber(arrayLength: Int) -> Int {
var unsignedArrayCount = UInt32(arrayLength)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return randomNumber
}
// Importing plist File
let quotePath = NSBundle.mainBundle().pathForResource("yourPlistName", ofType: "plist")
let dictionary: NSDictionary = NSDictionary(contentsOfFile: quotePath!)!
let wordDictionary: Dictionary = dictionary
let wordStringArray = [String](map(wordDictionary.keys) { $0 as AnyObject as String })
// Selects Quote
let chosenWord = wordStringArray[randomNumber(wordDictionary.count)]
let chosenDefinition = wordDictionary[chosenWord] as NSString as String
// Assigns Quote & Author to IBOutlet
word = chosenWord
definition = chosenDefinition
wordLabel.text = word
definitionLabel.text = definition
}
I hope this helped, if you have any other questions don't hesitate to ask!
Ozzie