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

jim vlagas
jim vlagas
3,700 Points

is this solution right?

func isDivisilbe (#numberEna: Int, #numberDio: Int) -> (einai: Bool , description: String )? {

    var einai = (false,"is not divisible")

    if (numberEna % numberDio) == 0{


        einai = (true," is divisilbe")

    }

    return einai

}

if let divisible = isDivisilbe(numberEna: 5, numberDio: 2){

    println(divisible.description)

    println(divisible.einai)
}

i tried some times and its ok am i right?

1 Answer

Chris Shaw
Chris Shaw
26,650 Points

Hi Jim,

The solution you've given is over complicated as per what the challenge is asking for, currently you're using named parameters which isn't required and you're returning a tuple which isn't the required return type, you simply need to return a Bool optional which as the tasks specifies is either true or nil.

See the below code which is similar to your code but follows the task given.

func isDivisible(one: Int, two: Int) -> Bool? {
    if one % two == 0 {
        return true
    }

    return nil
}

if let divisible = isDivisible(10, 5) {
    println("Divisible")
}

Please bare in mind your answer is valid but again it doesn't follow along with the task at hand, let me know if any of this is confusing.

Happy coding!