IBOutlets and IBactions require ! in the end

First of all get to know, what is actually ! and ?

  • Use ? : if the value can become nil in the future, so that you test for this.
  • Use ! : if it really shouldn't become nil in the future, but it needs to be nil initially.

@IBOutlet:

When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil.

In effect, the compiler replaces @IBOutlet var name: Type with @IBOutlet weak var name: Type! = nil.

Xcode would change it and Force restrict on declare @IBOutlet non option type variable , so following both kind of declaration for @IBOutlet is Valid till date.

@IBOutlet var outputLabel : UILabel!
@IBOutlet var priceLabel : UILabel?

However, if you control drag an outlet for a label in beta 4 this happens:

@IBOutlet var priceLabel : UILabel! = nil

Interface builder data is loaded after view controller has been initiated, so outlets cannot have value after initialisation. With implicitly unwrapped optional properties (outlets in this case) you promise that properties might be nil after object is initiated, but their value will be assigned later (after the load of nib or storyboard).


That is correct. In Swift, a variable of type X cannot be nil, meaning it has to be initialized. This means that you must initialize either in a init method, or inline initialize.

Generally, view controllers will declare variables of types that are optional - for example,

@IBOutlet var outputLabel : UILabel!

This means that you do not need to initialize the outputLabel, and by default, it's value is nil. This is the general pattern for IBOutlet variables, as the variables are set outside of the init method.

If you do not make your variables optional, you must initialize it. If you do not initialize inline, you must provide an init method - hence the error that you are getting.