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 Exercise: isDivisible function

james oliver
james oliver
Courses Plus Student 684 Points

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


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.