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 Review: Functions

Leonardo Cavalcanti
Leonardo Cavalcanti
5,308 Points

Optionals swift basics return bool

Hello everyone, I don`t exactly know why this isn't working.

divisible.swift
func isDivisible(#dividend: Int, #divisor: Int) -> Bool {
    if dividend % divisor == 0 {
        return true
    } else {
        return false
    }
}
func isNotDivisible(#dividend: Int, #divisor: Int) ->Bool? {
    if dividend%divisor != 0 {
    return true
    }
    else {
    return nil
    }
}

1 Answer

Change return type to Bool only ( not Bool?). And change nil to false in return statement :

func isNotDivisible(#dividend: Int, #divisor: Int) ->Bool { if dividend%divisor != 0 { return true } else { return false } }

Leonardo Cavalcanti
Leonardo Cavalcanti
5,308 Points

yep this worked, I just don't see the point of this challenge as you don't even use optionals for it... but thank you a lot.

Emil Rais
Emil Rais
26,819 Points

If nothing else, Leonardo, take it as a lesson. There's a right time to use optionals and there's a right time to stay far away. Optionals provide great safety, but when they are added in unnecessary situations, they introduce a non-trivial source of danger and clutter to your code.