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

iOS Build a Simple iPhone App with Swift Creating a Data Model Finishing Up Our Model

How 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?

Thomas Lahoud
Thomas Lahoud
6,092 Points

I 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?

Drew Carver
Drew Carver
5,189 Points

Was wondering the same..

1 Answer

Michael Liendo
Michael Liendo
15,326 Points

here 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]
    }

Thanks Michael, after taking a break for a few hours and coming back it makes sense what he's doing.