0

I am trying to update an UILabel.

@IBOutlet weak var balanceLabel: UILabel!

it is initially declared as a non optional variable:

var balanceValue: Int = 1

The UILabel depends on the not optional variable balanceValue.

func updateBalanceLabel() {
        balanceLabel.text = String(balanceValue) }

That all worked fine, but at some point I got this fatal error, which wont go away:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

The error is referring to this line:

balanceLabel.text = String(balanceValue)

That does not make any sense... I am having a non optional variable which is declared as 1 per default. How can it be nil.

Here is the crashlog:

> libswiftCore.dylib`_swift_runtime_on_report:
->  0x7fff51b801c0 <+0>: pushq  %rbp 
    0x7fff51b801c1 <+1>: movq   %rsp, %rbp
    0x7fff51b801c4 <+4>: popq   %rbp
    0x7fff51b801c5 <+5>: retq   
    0x7fff51b801c6 <+6>: nopw   %cs:(%rax,%rax)

and

ibswiftCore.dylib`Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never:
    0x7fff518d9370 <+0>:  pushq  %rbp
    0x7fff518d9371 <+1>:  movq   %rsp, %rbp
    0x7fff518d9374 <+4>:  movl   0x28(%rbp), %eax
    0x7fff518d9377 <+7>:  movl   0x18(%rbp), %r10d
    0x7fff518d937b <+11>: pushq  %rax
    0x7fff518d937c <+12>: pushq  0x20(%rbp)
    0x7fff518d937f <+15>: pushq  %r10
    0x7fff518d9381 <+17>: pushq  0x10(%rbp)
    0x7fff518d9384 <+20>: callq  0x7fff51adf410            ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never
->  0x7fff518d9389 <+25>: addq   $0x20, %rsp
    0x7fff518d938d <+29>: ud2    
    0x7fff518d938f <+31>: nop

What weirds me out, that I did not change the working code, but now all of a sudden I get this problem. Restarting XCode wont solve it. Tried different things to pin down the problem, but I cant find the reason why balanceValue should be nil. Any help greatly appreciated.

Roman P.
  • 13
  • 6
  • can you sen full code of that file ... – Shivam Parmar Jan 28 '20 at 06:19
  • Can you add a breakpoint and check what is the value of `balanceValue` in the function `updateBalanceLabel`? – Rob Jan 28 '20 at 06:24
  • try to check `balanceLabel` @IBOutlet connected to storyboard – Kishan Bhatiya Jan 28 '20 at 06:27
  • @Rob I dont know how to use breakpoints. But I added a print right before the line of the error: 'func updateBalanceLabel() { print(balanceValue) balanceLabel.text = String(balanceValue) }' And it prints 111 – Roman P. Jan 28 '20 at 06:33
  • This is the console: 111 'Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/roman/Desktop/vToken/vToken/ViewController.swift, line 87 2020-01-28 13:30:18.234060+0700 vToken[5719:617245] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/roman/Desktop/vToken/vToken/ViewController.swift, line 87 (lldb) ' – Roman P. Jan 28 '20 at 06:34
  • Check your `storyboard` you must not have connected the `IBOutlet`. – Rob Jan 28 '20 at 06:34
  • @KishanBhatiya Yes it is connected https://imgur.com/a/m0hSPpf – Roman P. Jan 28 '20 at 06:37
  • @Rob I just rechecked, disconnected, and connected it again. Still same error... – Roman P. Jan 28 '20 at 06:39
  • @RomanP. Can you try this: `balanceLabel.text = "\(balanceValue)"`? – Rob Jan 28 '20 at 06:43
  • @Rob balanceLabel.text = "\(balanceValue)" still throws the same fatal error... – Roman P. Jan 28 '20 at 06:49
  • @RomanP. Thanks for the update. I guess now it worked for you. – Rob Jan 28 '20 at 06:50
  • Anybody knows why the outled is broken??? – Roman P. Jan 28 '20 at 06:54
  • @RomanP. now you got solution but once try to check create another label and assign value to it – Kishan Bhatiya Jan 28 '20 at 07:07

2 Answers2

1

Check balanceLabel if nil or not, balanceValue is not optional so the error which you are getting is due to balanceLabel only. Use blow method


func updateBalanceLabel() {

     guard let label = self.balanceLabel else{
       print("balanceLabel found nill ")
       return 
   }
    label.text = String(balanceValue)


}

After executing this if you are getting balanceLabel found nill then you should check outlet & connect it to UILabel in storyboard.

Afsar edrisy
  • 1,985
  • 12
  • 28
0

Its nil because your outlet is broken. Since you declared it as an implicitly unwrapped optional, balanceLabel.text = String(balanceValue) is the same as balanceLabel!.text = String(balanceValue). You can verify this is tru by simply using optional unwrapping (an implicitly unwrapped optional is still an optional): balanceLabel?.text = String(balanceValue). Go into your storyboard, right click the label and look at the connections. break any existing connections and then recreate the binding to your variable.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33
  • Thanks. Exactly as you said. Once I added the ? to balannceLabel?.text, the error jumped to the next function. In the end I had to add ? to all functions. Breaking and recreating bindings to variable did nothing for me tho. Now the question remains why it happened all of a sudden. First it worked fine the way I wrote it. – Roman P. Jan 28 '20 at 06:45