josmek
josmek

Reputation: 211

'IBOutlet' property has non-optional type 'UIButton'

Here's 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.
    }

}

It's a simple IBOutlet (straight from the Apple developer docs). It gives me the error "'IBOutlet' property has non-optional type 'UIButton'" and I have no idea how to fix it.

Upvotes: 7

Views: 6715

Answers (3)

Varsha Vijayvargiya
Varsha Vijayvargiya

Reputation: 771

class SecondViewController: UIViewController {

     @IBOutlet weak var name: UILabel! = nil

     override func viewDidLoad() {

           super.viewDidLoad()

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

This code working fine

But when i replace @IBOutlet weak var name: UILabel? it is not working.

Upvotes: 0

idmean
idmean

Reputation: 14905

It should be like that (in Beta 3 or before):

@IBOutlet var button: UIButton?

IBOutlets must be optionals so place a ? behind the type.

Upvotes: 12

Avi
Avi

Reputation: 2216

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

Upvotes: 11

Related Questions