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
Harold Davis
Courses Plus Student 14,604 PointsReturning Dictionaries from a function?
So I'm trying to return a random dictionary from an array of dictionaries using a function in Swift.
Here is the part not working:
//method for returning a random array from the collection sourse without doing it twice...
func noRepeatArray(array: Array) -> Dictionary{
let newRandomNumber = randomNumber()
let randomSet = array[newRandomNumber]
let canUseSet: Bool
if(temp.contains(randomSet)){
newRandomNumber == randomNumber()
canUseSet = false
} else {
temp.append(randomSet)
canUseSet = true
}
if canUseSet == true {
return randomSet
}
}
Here is the code around it:
//Slot Types for Button
enum slotNumber {
case slotOne
case slotTwo
case slotThree
case slotFour
}
//Historical Events dictionaries
let appleHistoryI: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
let appleHistoryII: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
let presidants: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
let gameOfThrones: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
//collection of dictionaries...
let collectionOfDictionaries = [
appleHistoryI,
appleHistoryII,
presidants,
gameOfThrones
]
//random number method
func randomNumber() -> Int{
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(collectionOfDictionaries.count)
return randomNumber
}
//method for returning a random array from the collection sourse without doing it twice...
func noRepeatArray(array: AnyObject) -> AnyObject{
let newRandomNumber = randomNumber()
let randomSet = array[newRandomNumber]
let canUseSet: Bool
if(temp.contains(randomSet)){
newRandomNumber == randomNumber()
canUseSet = false
} else {
temp.append(randomSet)
canUseSet = true
}
if canUseSet == true {
return randomSet
}
}
//temperary collection to keep track of used arrays
let temp = []
Is there a better way i should do this or do i need to figure a way to return dictionaries?
2 Answers
Robert Baghai
5,492 PointsTry this to get your random number...
func noRepeatArray(array: Array) -> Dictionary {
let newRandomNumber = random() % array.count
return array[newRandomNumber]
}
It will always return a random number that works without going outside the bounds of the array i.e. it won't crash.
Also, in your function that you shared you never actually returned a dictionary at the end of it.
Robert Baghai
5,492 PointsActually you do return a dictionary but only if the following condition works..
if canUseSet == true {
return randomSet
}
I think the random function i used is better though.
Harold Davis
Courses Plus Student 14,604 PointsI was wondering if it would work hmm
Harold Davis
Courses Plus Student 14,604 PointsHarold Davis
Courses Plus Student 14,604 PointsThanks man ill give that a try and I started to notice I wasn't returning a dictionary when I went back lol I was like noooo!