Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Optionals Exercise: isDivisible function

Sameer Vohra
Sameer Vohra
754 Points

If let necessary?

Is this right? Seems like it works, but I didn't use the "if let" portion. Do I need to, why?

func isDivisble (#number1: Int, #number2: Int) -> Bool? {
    if number1 % number2 == 0 {
        println("Divisible")
    } else if number1 % number2 != 0 {
        println("Not Divisible")
    }
      return nil
}

isDivisble(number1: 15, number2:5)

2 Answers

The keyword “let” is used to define a constant. This is told in one of the first iOS courses. Since you’ve already defined your variables you don’t need to use the “let” keyword (or the “var” keyword for that matter)

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

Yes, you have to add the 'if let' before calling the function. In the playground this works without 'if let' but in a real world project this wouldn't do anything.

So in your case it would look something like this

func isDivisble (#number1: Int, #number2: Int) -> Bool? {
    if number1 % number2 == 0 {
        println("Divisible")
    } else if number1 % number2 != 0 {
        println("Not Divisible")
    }
      return nil
}

if let result = isDivisble(number1: 15, number2:5){
//do something like println(result)
}