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

james oliver
PLUS
james oliver
Courses Plus Student 684 Points

Two Questions. Can we only return nill as an optional? constant vs var

Hi,

I would like to know if we only can return at the end of the function nil when we use optional. Because if I want to return string at the end I can't. Thanks

Second.. its a question that I have long time ago. I'm a web developer and I rarely use Constants. My question is. Whats the difference using constant rather than a variable. I know the technically difference but. Why not only vars? I know that I'm missing something here and I would like to know what is your opinion.

Cheers! and Thanks in advance!

1 Answer

To answer your first question: Optionals are not required, that is why it's called an optional. How could you have two required parameters but return an optional output? That would not make sense. You can return multiple values in a function using tuples.

Example: func isDivisible(num1: Int, num2: Int) -> (Bool?, String) { //some code }

(Bool?, String) specifies a tuple as a return, with the bool value being the optional, and the String being required. When typing out your return statement you would then return (true, "Divisible"). Although you are able to do this, for this specific challenge, that is not the solution.

I suggest you go back and review previous videos and read the additional material provided in the video descriptions.

Your second question can be answered by either doing a quick google search or going back and re-reading.