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 trialAaron Williamson
10,421 PointsHow does Pasan know to cast the arc4random_uniform as an Int?
It already looks like an Int before he casts it so how do you know to cast as an Int before proceeding?
Drew Carver
5,189 PointsWas wondering the same..
1 Answer
Michael Liendo
15,326 Pointshere is my commented code, hope this helps :)
func randomFact() -> String {
//the count property returns an Int so first we cast it to an uInt32
var unsignedArrayCount = UInt32(factsArray.count)
//arc4random_uniform takes in the Uint32 and returns a random number
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
//we then take the random number and convert it back to an int so we can use it as a subscript in our array.
var randomNumber = Int(unsignedRandomNumber)
return factsArray[randomNumber]
}
Aaron Williamson
10,421 PointsThanks Michael, after taking a break for a few hours and coming back it makes sense what he's doing.
Thomas Lahoud
6,092 PointsThomas Lahoud
6,092 PointsI was also wondering this. I think it has something to do with "typecasting" through a function/method rather than putting (Int) at the start of the line?