[swift-evolution] Pitch: Modify the meaning of IBOutlet to remove the ! from the type

Daniel Steinberg daniel at dimsumthinking.com
Wed May 18 08:22:45 CDT 2016

I would like to propose we modify the meaning of @IBOutlet in light of the accepted proposal SE-0054 Abolish ImplicitlyUnwrappedOptional type. I think this fits in with the current Swift 3 goals.

Currently we use “var” and “!” when we declare an outlet like this:

@IBOutlet weak var myLabel: UILabel!

The “!” in the declaration allows us to use the outlet like this without unwrapping it

myLabel.text = “Hello"

We use “var” and “UILabel!" because myLabel starts its life out as nil and does not have a value until the connection is made. i.e. myLabel must be an optional if it accepts nil and the type is UILabel! instead of UILabel? so that we don’t have to unwrap it each time we use it.

If we break the connection to the UILabel instance we crash at runtime for attempting to unwrap nil.

Given this, I propose that we be able to write

@IBOutlet weak let myLabel: UILabel

In this case @IBOutlet has a meaning somewhat similar to lazy - it’s not that myLabel doesn’t exist until we first call it, but @IBOutlet indicates that myLabel should exist before we call it. 

If the connection isn’t made and myLabel doesn’t exist, we should crash as we do now. If the connection is not made in the nib or storyboard, this will crash at development time. 

This removes a case in which we use var - not because we want to change the value of a property but because of a detail in the tooling.

This change also removes a case in which we use an Optional again for a detail in lifecycle and tooling.



More information about the swift-evolution mailing list