Property "IBOutlet" has an optional type "UIButton"

Here is my code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var button: UIButton

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

This is a simple IBOutlet (straight from Apple developer docs). This gives me the error "IBOutlet property" has the optional type "UIButton", and I have no idea how to fix it.

+4
source share
3 answers

It can also be -

    @IBOutlet var button: UIButton! 

or

    @IBOutlet var weak button: UIButton! (in case you are not doing view unloading)

if you are using XCODE 6 Beta 4

+7
source

It should be like this (in Beta 3 or earlier):

@IBOutlet var button: UIButton?

IBOutlets must be optional, so place ?them behind the type.

+10
source
class SecondViewController: UIViewController {

     @IBOutlet weak var name: UILabel! = nil

     override func viewDidLoad() {

           super.viewDidLoad()

           name.text="i m a label"
     }
 }

@IBOutlet var name: UILabel? .

0

All Articles