SLN
SLN

Reputation: 5092

Assign a nil to a forced unwrapping optional doesn't crash the code, why is that?

I'm on the initial way of building a calculator. Currently, the code is doing nothing but printing the digits and Pi into the calculator's label while user taps it.

1) Che Code

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var display: UILabel! = nil //Look Here "(

    var userIsInTheMiddleOfTypeing = false

    @IBAction func touchDigit(_ sender: UIButton){
        let digit = sender.currentTitle!
        if userIsInTheMiddleOfTypeing {
            let textCurrentlyInDisplay = display.text!
            display.text = textCurrentlyInDisplay + digit
        } else {
            display.text = digit
        }

        userIsInTheMiddleOfTypeing = true
    }

    @IBAction func performOperation(_ sender: UIButton) {
        userIsInTheMiddleOfTypeing = false
        if let methematicalSymbol = sender.currentTitle {
            if methematicalSymbol == "π" {
                display.text = String(M_PI) // M_PI
            }
        }
    }      
}

2) UI

The touchDigit function is linked to all the digit buttons as shown in the following figure. The display is the UILable while performOperaton is the PI button

enter image description here

Question: You may found the line (Third line of the code) UILabel! = nil. I think adding a nil to a forced unwrapping optional would crash the code, but it doesn't and so far, the code works fine. Why is that?

Upvotes: 0

Views: 158

Answers (3)

Grimxn
Grimxn

Reputation: 22507

Unwrapped optional is a contract with the compiler that the value won't be nil when it is accessed. Consider the following playground code.

var x: Int! = nil // It's declared as an unwrapped optional *var*
x = 5 // So assigning to it is OK
var y: Int = x // you can access its value, if it's not nil
x = nil // even assigning nil is OK
//y = x // CRASH. *Accessing* it when nil causes an error

Adding = nil in your declaration is actually spurious - if you dropped it, the variable would still be initialised as nil, and, as @MartinR pointed out in comments, your outlet will be assigned to when the Xib is loaded.

Upvotes: 5

MacUserT
MacUserT

Reputation: 1953

When I look at your UI under point 2 I see that you have connectie the UILabel to a label in your storyboard. Is that correct? If this is correct, the application doesn't crash when you set it to nil when declaring your UILabel. As soon as the view lifecycle methods are running the UILabel declared and initially set to nil, will be connected to the UILabel in your storyboard.

You can check this by implementing the view lifecycle methods and let it print the UILabel to the console.

Upvotes: 1

Yury
Yury

Reputation: 6114

When you declare variable as forced unwrapped optional

var labelForced: UILabel! = nil

all difference from regular optional variable

var label: UILabel? = nil

is that forced unwrapped optional will always try to implicitly unwrapp when you will try to get it value (except case you will mark explicitly safe unwrapp, for example let text = labelForced?.text). All other behavior is same

Upvotes: 1

Related Questions