-
Notifications
You must be signed in to change notification settings - Fork 3
Optionals
Pablo Villar edited this page Mar 20, 2017
·
3 revisions
Variables must always be defined with an appropriate optionality, following what's described in this blogpost.
Good
@IBOutlet weak var titleLabel: UILabel!
self.titleLabel.text = "Something"
// If `titleLabel` is `nil`, app will crash at runtime, making it obvious that the developer is forgetting to hook it up in the Interface Builder.
Bad
@IBOutlet weak var titleLabel: UILabel?
if let label = self.titleLabel {
label.text = "Something"
}
// Conceptually wrong. Developer may forget to hook `titleLabel` in Interface Builder and that mistake could easily make it to a production stage without being noticed.
- It helps catch bugs earlier in the development stage.
- Variables meaning and their intentions are clearer.