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
Luke Ayres
2,971 PointsDesign for ensuring same color doesn't repeat
var colorArrayNumber = 0
mutating func getRandomColor() -> UIColor {
var randomNumber: Int
repeat {
randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(colors.count)
}
while(randomNumber == colorArrayNumber)
colorArrayNumber = randomNumber
return colors[randomNumber]
}
Above is the code from the video that I've modified to avoid repitition of the background color (I've played a similar trick with the facts), my question is is this poor design? Is there a better way to do this?
henriquegiuliani
12,296 Pointshenriquegiuliani
12,296 PointsHey Luke, there's an easier way to guarantee an unique value for each array index. Check out for SET Collections. A Set is a type of collection that cannot accept repeated values.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html
Hope it helps.