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 Functions and Optionals Optionals Exercise: isDivisible function

What is if- let about? How do I think of this in simple terms?

I see what it does in the video, but how do I think about this so I can do it myself in my own code. Simple terms are always appreciated.

3 Answers

Logan R
Logan R
22,989 Points

What he is doing is declaring a variable inside of an if statement. I would not really recommend doing that personally. You can write the same thing like this:

let result = isDivisible(dividend: 15, divisor: 3)

if result {
    println("True")
} else {
    println("False")
}

Does that make more sense?

Edit, You may need to check if result is true / false so:

let result = isDivisible(dividend: 15, divisor: 3)

if result == true {
    println("True")
} else {
    println("False")
}

Result is a boolean so it will be true or false.

Is it basically follows the order of the last function right? I mean we did not declare if result == something then this. I think I get it now.

Logan R
Logan R
22,989 Points

result will be equal to a boolean so true or false. You may need to do if result == true. I haven't worked with swift in a few months lol, sorry (It's valid in Java).

Thanks for your reply though, you're a helpful person :)

Logan R
Logan R
22,989 Points

No problem! :)