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 trialLeonardo Cavalcanti
5,308 PointsOptionals swift basics return bool
Hello everyone, I don`t exactly know why this isn't working.
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
Farhad Mohammad Afzali
5,007 PointsChange 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
5,308 PointsLeonardo Cavalcanti
5,308 Pointsyep 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
26,873 PointsEmil Rais
26,873 PointsIf 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.