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

Found nil when displaying artists

When I run the application and then press the button that will take you to the detail view, the app crashes and returns this error "fatal error: unexpectedly found nil while unwrapping an Optional value". The error occurred at the "playlistArtist0.text = playlist!.artists[0]" line. So I replaced the corresponding outlet, and it worked (even though the outlet I replaced it with was identical. " @IBOutlet weak var playlistCoverImage: UIImageView!" It then worked. Also, before that I commented out the "playlistArtist0.text = playlist!.artists[0]" line and it displaced the other two artists just fine. But, now I am getting that error again, except it is now at "playlistArtist2.text = playlist!.artists[2]". What is going wrong?

My playlistDetailViewController code:

import UIKit

class PlaylistDetailViwControllerViewController: UIViewController {

@IBOutlet weak var buttonPressLabel: UILabel!
var playlist: Playlist?

@IBOutlet weak var playlistCoverImage: UIImageView!
@IBOutlet weak var playlistTitle: UILabel!
@IBOutlet weak var playlistDescription: UILabel!



@IBOutlet weak var playlistArtist0: UILabel!
@IBOutlet weak var playlistArtist1: UILabel!
@IBOutlet weak var playlistArtist2: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    if playlist != nil {
        playlistCoverImage.image = playlist!.largeIcon
        playlistCoverImage.backgroundColor = playlist!.backgroungColor
        playlistTitle.text = playlist!.title
        playlistDescription.text = playlist!.description

        playlistArtist0.text = playlist!.artists[0]
        playlistArtist1.text = playlist!.artists[1]
        playlistArtist2.text = playlist!.artists[2]
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} Thanks, Kyle

3 Answers

I figured it out!

How to solve it? Thks!

Did you have the same problem?

I had just incorrectly linked the artist labels to the code, so I just residing those and it worked.

yes, me too. ^_^