1

I'm getting the following error when i try to update a label in swift.

fatal error: unexpectedly found nil while unwrapping an Optional value

There must be something I am missing. These are the two variables involved:

Label and Double variable

And this is the func where I seem to get the error:

Method to update label

As far as I can see the variable that I want to update the label with has a value of 19.875531899929 and I want the label to show the Int part of that. Where does the nil come from? I am stuck finding the bug.

Can the fact that I called the method from a ViewController instance in the applicationDidBecomeActive method of AppDelegate.swift have anything to do with it? This is the short code to call the method:

func applicationDidBecomeActive(application: UIApplication) {         

    viewControllerInstance.convertAlertTimeToCountdownFromMinutes() 

}

And the variable in AppDelegate.swift:

var viewControllerInstance = ViewController()
Niklas
  • 75
  • 2
  • 9

1 Answers1

2

Your UILabel object is nil, you forgot to connect your IBOutlet with your viewController's Label, try to connect the IBOutlet with UILabels named lblMinutes and lblMinutesTag.

New Edit

If you want to update the value of label when your app become active you can use NSNotificationCenter for that, add below code inside your viewController.

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationBecameActive:",
           name: UIApplicationDidBecomeActiveNotification,object: nil)
}

func applicationBecameActive(notification: NSNotification){
    self.convertAlertTimeToCountdownFromMinutes() 
}

deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • I'm not sure. I have Ctrl-dragged both labels to **ViewController.swift**. Can the fact that I called the method from a ViewController instance in the `applicationDidBecomeActive` method of **AppDelegate.swift** have anything to do with it? – Niklas Aug 06 '16 at 14:02
  • You are calling this `updateLblMinutes()` method from **AppDelegate.swift**? Can you show the code that you have written inside `applicationDidBecomeActive` method. – Nirav D Aug 06 '16 at 14:13
  • Certainly. It's not much but: func applicationDidBecomeActive(application: UIApplication) { viewControllerInstance.convertAlertTimeToCountdownFromMinutes() } – Niklas Aug 06 '16 at 15:21
  • Hmm ... that lost all its formatting during save and I can't find how to add images in comments. – Niklas Aug 06 '16 at 15:22
  • Okay, I have edited the question. i'm not sure if I'm supposed to write that or if you get some kind of update automatically. – Niklas Aug 06 '16 at 15:50
  • How should it be declared to not produce nil in the label? – Niklas Aug 06 '16 at 16:06
  • Check my edit, Also remove the code that you have written inside `AppDelegate` methods. – Nirav D Aug 06 '16 at 16:18
  • It works. :-) Thank you very much for all your help! – Niklas Aug 06 '16 at 16:32