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 trialJosh Ko
1,510 PointsClarity on "If - let" in unwrapping an optional
Aside from wanting to store the result, why do we use "let result =" to unwrap an optional? It just seems to be a very cumbersome way to do things.
If "let result = isDivisible(dividend: 11,divisor: 2)" and "isDivisible(dividend: 11,divisor: 2)" both equal nil why do I get an error when I try to use the latter in the if statement.
1 Answer
Max Hirsh
16,773 PointsIf I understand it correctly, the isDivisible() function you use returns true or false depending on whether the dividend is divisible by the divisor? In that case, you would have an error because the if let method unwraps and stores an optional as a variable. You wouldn't be able to store the result to a boolean. That would sort of be like saying "the value of True is 7" or something. If you were trying to compare the optional logically after unwrapping it, I would recommend storing it as a variable first, then doing a comparison.
Dennis Parussini
Treehouse Project ReviewerDennis Parussini
Treehouse Project ReviewerCould you post the code you are referring to please?