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 Using a Navigation Controller With Segues Modifying the UI

fatal error: unexpectedly found nil while unwrapping an Optional value

Getting error on this line: buttonPressedLabel.text = segueLabelText

I tried writing the code myself as well as running the provided xcode project files, I'm still getting the error. Just for funsies I deleted the label and created a new one, still getting the error.

Any help would be greatly appreciated.

3 Answers

Do you have segueLabelText set to an empty string

import UIKit

class PlaylistDetailViewController: UIViewController {

    @IBOutlet weak var buttonPressLabel: UILabel!
    var segueLabelText: String = ""
    //rest of code .................    
}
Iván Jesús Fanjul Cosént
Iván Jesús Fanjul Cosént
3,882 Points

Hello Sean,

here i got the same problem, but taking a look for a while I just found that for some reason my buttonPressedLabel just lost the reference to the outlet.

If you go to your Main.storyboard and click the label in the Artist Scene and click on the Show Connectors inspector, you are going to notice that there is no reference to your label.

As you can see I just made a test code like this: added a new reference to the label, and tried it again

'''swift

@IBOutlet weak var buttonPressLabel: UILabel!
var segueLabelText: String = ""

@IBOutlet weak var test: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

   // buttonPressLabel.text = segueLabelText
    test.text = segueLabelText  
}

'''

It works for me, now i just need to delete the old reference and the text one and add it again . Hope this helps

Iván Jesús Fanjul Cosént
Iván Jesús Fanjul Cosént
3,882 Points

(Sorry me, i don't know how to post swift code, any example would be apreciated)

I had the same problem and fixed it my creating a new outlet for the label.