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
Italo Borges
7,238 PointsCreating an if statement for nil exception
Hello, I'm trying to put some text to a label outlet.
I tried to do the same that I saw in the video.
Declaring my Outlet in PlaylistDetailViewController:
@IBOutlet weak var messageWhenPressed: UILabel!
Update label text in method viewDidLoad:
messageWhenPressed.text = "Some text"
So, when I try this approach, I'm getting this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I checked if my label is connected to the my Outlet and it is.
To solve my problem, I'm having to do inside the method viewDidLoad:
if let field = messageWhenPressed {
field.text = segueLabelText
}
Someone can tell me why?
2 Answers
Jhoan Arango
14,575 PointsHello:
Well from what I can see you are trying to show a message on a label, and the system is complaining that there is no message to put in the label. The fix you are giving it, is the correct one when working with optionals, but I am sure that's not what your intentions are. I would be able to help you further if I was able to see more of your code, you are only showing the results of what you want, but not the origin of it. For example, on your fix there is a segueLabelText, where is that coming from ?
If you could show some more of your code, maybe I can help.
:)
Italo Borges
7,238 PointsSure! :) italoborges@gmail.com
Jhoan Arango
14,575 PointsInvite sent, look for a "slack" e-mail.
Italo Borges
7,238 PointsItalo Borges
7,238 PointsJhoan, your answer helped me to think about the problem and because of that I could figure out whats going on. The name's of Master View Controller was wrong, I forgot to update. My mistake! Thanks!
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsOh great perfect.. If you don't mind me asking. I am gathering a group of swift developers, or at least people who want to start collaborating in different projects.. This is with the intentions of practicing beyond treehouse's material. Perhaps you are interested ? if so you can send me your e-mail and I can send you an invite to the group. We are doing it to practice and learn how to collaborate in github and use other tools.