Initialize attributes in didSet

suggest change

The outlet is title. The original didSet assigned label, which is not that property. This is Swift 2 (redColor(), systemFontOfSize, .TouchUpInside). A property’s initial value does not run didSet, so the button’s target is not added for the UIButton() created on the next line. didClickLogin must exist on LoginController. Naming the outlet title also hides UIViewController.title.

@IBOutlet weak var title: UILabel! {
  didSet {
    title.textColor = UIColor.redColor()
    title.font = UIFont.systemFontOfSize(20)
    title.backgroundColor = UIColor.blueColor()
  }
}

It’s also possible to both set a value and run code when a later assignment replaces it:

private var loginButton = UIButton() {
    didSet(oldValue) {
        loginButton.addTarget(self, action: #selector(LoginController.didClickLogin), forControlEvents: .TouchUpInside)
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents