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

swift code explained please random numbers

Hi all, just watched the swift iOS video on finishing up our model. can anyone please explain this swift code step by step (idiots guide for me please) as its not really explained in the video when its re-written .

i understand what the block of code does and i sort of understood when it was 1st written in one line but then got confused when its written into separate lines. many thanks

//heres the code

func randomFact() -> string { var unsignedArrayCount = UInt32(factsArray.count) var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount) var randomNumber = Int(unsignedRandomNumber)

}

2 Answers

Nathan Tallack
Nathan Tallack
22,164 Points

My comments inline with your code below.

// Here we have a function that we call passing in no parameters and it returns a String type.
func randomFact() -> String {
    // First we declare unsignedArrayCount as the Int that is the number facts in our factArray.
    var unsignedArrayCount = UInt32(factsArray.count)
    // Next we declare unsignedRandomNumber as a random number from 0 to the factArry count.
    var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
    // Next we get that unsigned random Int and make it a regular signed Int.
    var randomNumber = Int(unsignedRandomNumber)

    // Missing from your code is the return.
    // Here we will return the random fact from your factArray
    return factArray[randomNumber]
}

I hope this helps. :)

Thank you Nathan , still a little confused . Why the uint32 ? Thanks Jon

Nathan Tallack
Nathan Tallack
22,164 Points

This gets a little complex. Work with me here. It is worth it in the end. :)

When we call the .count method on the factsArray type (inherits from the Array class when we create the object) that method returns an Int of a type that can represent both positive and negative numbers.

We typecast it as a UInt32 (the U stands for unsigned) because that is the type of Int that the arc4random_uniform wants to take in as a parameter when we use it on the next line.

Then we get that and typecast it as a regular Int (which is a signed int, meaning it can be positive or negative) because that is the type of Int we need when we are using as an index in our return statement.

It is WONDERFUL that you are asking for these clarifications, because that is EXACTLY the kind of attention to detail that you need to get the most out of your education here.

Never be shy to ask the forum for answers like this. Remember, we are all either exactly where you are now or we once were and want to give you the same help that we got! :)

Many thanks for all your help Nathan. Great answer , well explained .