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 Build a Simple iPhone App with Swift Getting Started with Xcode Recap of Swift Basics

Jack Ferguson
Jack Ferguson
1,018 Points

When I type optionalstring without the bang it still displays "hello" in the right bar. The bang doesn't do anything.

I declared the optional thus: var optionalString: String? = "hello" and in the right bar "hello" is displayed

1 Answer

Michael Liendo
Michael Liendo
15,326 Points

when you type var optionalString: String? = "hello you're creating an optional (a variable that can either be nil or contain a value) and setting the value to a string (hello). When you use the bang, you're force-unwrapping the optional, telling the complier that when the variable is used it will always have a value (whether it be "hello" or otherwise). If you leave out the ? or the ! entirely, then it's no longer an optional, it's just a plain string with a value of hello. Hope that helps clear things up!