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 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