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 Build a Playlist Browser with Swift Refactoring Our Code Passing Playlist Objects Using Segues

Why are we putting the "let playlistDetailController" assignment inside of the if let statement?

In the video, when Pasan makes the if let statement, he moves this line of code into the if let statement.

let playlistDetailController = segue.destinationViewController as PlaylistDetailViewController

why did he do this? Does it have to be in there. I tried moving out and placing it before the if statement and the app still ran just fine?

1 Answer

In swift we deal with optionals which means that the data we are dealing with might be or returned " nil " and if it's nil the application could crash. Therefore when using "if let" it makes sure that you receive data before it executes the code block and prevent the application to fail. Hope that helps

Hi Meek, Thank you for the quick reply. I know the purpose of an if let statement I was just wondering why we put that specific statement inside. I tried to remove it and put it before and it still worked. Was just curious to see if there was a specific purpose of putting it inside of the if statement. Thanks again.

No problem man