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

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)
}