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 Building the Master and Detail Views Displaying Playlist Information

Erwin Gaitan Ospino
Erwin Gaitan Ospino
3,102 Points

Why using if playlist !=nil?

For validating optionals I thought the best way of doing that was:

if let playlist = playlist {
  // some code
}

but I see that Pasan uses:

if playlist != nil {
  // some code
}

What should be the correct way?

No reason - your suggestion is just as valid (and I think Apple encourages it). And it eliminates the need for unwrapping on every of the following lines. I don't know if I would use the same name though - or if it has any unwanted effects. In this case I would just use optional chaining like the following (to be fair, I think that's a new concept in Swift)

playlistCoverImage.image = playlist?.largeIcon