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 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,873 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.