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!

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 Functions and Optionals Optionals What is an Optional?


what is the significance of tempAptNumber if it is never used again

1 Answer

Marissa Amaya
Marissa Amaya
11,782 Points

tempAptNumber is what is called a "temp variable". Basically, it's a throwaway variable that serves one purpose, usually to hold a value while the program checks that value against another value, and then throws away the variable after the checking is done.

In this case, tempAptNumber is used to store a single apartment number while the for-in loop iterates through the array aptNumbers = ["101", "202", "303", "404"]. So, each time the loop iterates, tempAptNumber gets updated to the next value in the array. tempAptNumber is then used to see if it equals aptNumber (the apartment the user wants to find), and returns true if it matches. If there is no match, the function returns nil.

The pseudo-code can be written as follows:

//executing method

* In function, the for-in loop iterates through the array aptNumbers *

//loop 1
tempAptNumber = "101"
   //does tempAptNumber == aptNumber? No.

//loop 2
tempAptNumber = "202"
   //does tempAptNumber == aptNumber? No.

//loop 3
tempAptNumber = "303"
   //does tempAptNumber == aptNumber? No.

//loop 4
tempAptNumber = "404"
   //does tempAptNumber == aptNumber? Yes.
   return aptNumber

The significance of tempAptNumber is it is used to "snapshot" the current value in the array, which is then used to check if aptNumber exists in the array.