0

I need to fetch a NSString from the NSMutableDictionary and assign it to UITextField's text.

But I am getting an exception ; unexpectedly found nil while unwrapping an Optional value

When I log the string it writes to console.

My code is as below;

    let tempString:NSString! = tempDict.objectForKey("name") as? NSString

    NSLog("%@",tempString)

    cell.textLabel.text = tempString  //This line gives the exception
3
  • Well, if the string's okay, then it's probably the cell or the textLabel. Are you sure those aren't nil? Did you forget to connect one of them up in Interface builder? Commented Aug 20, 2014 at 8:23
  • show your declaration of the cell Commented Aug 20, 2014 at 8:25
  • var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell if (cell == nil) { cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier) } Commented Aug 20, 2014 at 8:25

2 Answers 2

1

You stop this by safely unwrapping cell.textLabel by adding an if statement.

if var label = cell.textLabel{
    label.text = "This is a title"
}

Hope this helps.. :)

Sign up to request clarification or add additional context in comments.

Comments

0

You can Try This. It Helped to get out from the same problem I was having

let userName : NSString = result.valueForKey("name") as! NSString self.lblUserName.text = userName as String

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.