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 Vending Machine App in Swift 2.0 Using the Vending Machine Displaying Icons with UIImage

Ryan Maneo
Ryan Maneo
4,342 Points

Why is force unwrapping so frowned upon?

Obviously in certain cases force unwrapping can be insecure... but for simple things such as forcing an animation of an asset upon launching the app or something it works fine and never returns an error for me... Optionals almost seem like more of a pain to me because of all you have to do just to get them to work!!!

From my understanding, if you force unwrap something like an option that has a value of nil, it could cause your program to crash because it is expecting some sort of data.

1 Answer

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Hey Ryan

It's not always going to return an error. And for extremely simple apps, it will work.

But as soon as you try to make the app more complex, or create more complex apps, you'll start to encounter crashes.

Look at it this way - the whole reason Optionals exist, is because they can be nil. If you force unwrap the optional, what's the point in using an optional in the first place? If you want to use an optional, it's possible the value can be nil, therefore you should write the code to check if it is nil.

I hope this helps you understand how important it is to not force unwrap optionals. Let me know if you have any other questions.

Regards

Simon