0
votes

When I try to set the a UILabel in Swift the app crashes and I get an error saying: "fatal error: unexpectedly found nil while unwrapping an Optional value" Here is my code, it crashes when I set the frame.

var newsFeed1: UILabel!
let Description = object!["Description"] as! String
newsFeed1.frame = CGRect(x: 22.5, y: 315, width: 475, height: 50)
newsFeed1.text = Description
storeTab.addSubview(newsFeed1)
1
where do you assign a value to newsFeed1? You are missing the line newsFeed1 = UILabel()!luk2302
Also check that whatever object is is not nilRich Tolley
@luk2302 Thanks I just noticed I forgot thatjacob

1 Answers

0
votes

Your code crashes because you try to unwrap the optional newsFeed1 which is nil - by using the ! you tell the program that there HAS to be something different than nil, which there is not. Since you never assign anything and apprently are not connecting it as an outlet either.

To fix the issue you have to assign something to newsFeed1 - before accessing any property of it you have to put the following line:

newsFeed1 = UILabel(frame: CGRect(x: 22.5, y: 315, width: 475, height: 50))