-4

Xcode gives me the following error but I have no idea where this is coming from. I printed elpasedTime value and it contains the value! But it still says it found nil when unwrapping and I have no clue why this happens since it doesn't tell me where.

Where could this possibly go wrong??

The function stringFromTimeInterval is as below

extension TimeInterval {

func stringFromTimeInterval() -> String {

    let time = NSInteger(self)

    //        let ms = Int((self.truncatingRemainder(dividingBy: 1)) * 1000)
    let seconds = time % 60
    let minutes = (time / 60) % 60
    let hours = (time / 3600)
    print("5 here?")
    return String(format: "%0.2d:%0.2d:%0.2d",hours,minutes,seconds)

}

}

enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Leonard
  • 2,978
  • 6
  • 21
  • 42
  • 3
    Your issue has nothing to do with your `stringFromTimeInterval` function. It's the `ViewController().timeLeft` access that is crashing. – rmaddy Jan 18 '19 at 18:15
  • 3
    Why are you trying to create and use a new view controller like this? There's no point. You create it, attempt (badly) to initialize it, then you throw it away unused. What are you actually trying to do? – rmaddy Jan 18 '19 at 18:19
  • @rmaddy I'm simply trying to update UILabel `timeLeft`. This function is accessing the label from outside the class. Perhaps that is where I got wrong? Should I place it somewhere else? – Leonard Jan 18 '19 at 18:23
  • 1
    In both places you have `ViewController().timeLeft.text` delete everything before `timeLeft` to make it `timeLeft.text`. Depending on when you call it you may still get nil for `timeLeft` but it won't be for long. – theMikeSwan Jan 18 '19 at 18:23
  • 3
    @Leonard, never try to mess with outlets from outside the owner. Xcode should mark them all as `private` so people don't make those kinds of mistakes. – theMikeSwan Jan 18 '19 at 18:24
  • I just solved the problem by putting the function inside class! – Leonard Jan 18 '19 at 18:26
  • 2
    For the record, as @rmaddy points out, `ViewController()` makes a _new_ instance of the class, it doesn't grab an existing instance so even if `timeLeft` wasn't nil it wouldn't do what you want it to. – theMikeSwan Jan 18 '19 at 18:26
  • Thank you all for helping out. I will answer my own question here. – Leonard Jan 18 '19 at 18:27
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Cristik Jan 19 '19 at 05:18
  • @Cristik Please read all the comments and the question before you mark this duplicate. The problem here is not that I don't understand what the error message means but more of why this happens. The link you gave me is thus not helpful at all. I edited the title so please remove the duplicate mark or replace it with something relevant. – Leonard Jan 20 '19 at 06:13
  • 1
    @Leonard the answers to the linked question provide several of possible reasons and solutions, check them out. – Cristik Jan 20 '19 at 07:21

1 Answers1

-3

Solved it.

As the comments pointed out, the problem was that I was not accessing ViewController correctly.

In order to access my ViewController outside of the ViewController class, I was creating a new instance of it by ViewController().

I solved it by putting the function inside the class and changing ViewController() part to self.ViewController.

This answer also helped me as well. https://stackoverflow.com/a/45932084/7414387

Leonard
  • 2,978
  • 6
  • 21
  • 42