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

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,676 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!