0

Can someone help me figure out why I can't fix this error in Swift using Xcode6? I'm trying to initialize all my buttons so they aren't nil and it's telling me I'm not declaring something when I thought I was. Any suggestions? The required init method line is giving me the error.

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var displayLabel: UILabel!
    @IBOutlet weak var buttonOne: UIButton!
    @IBOutlet weak var buttonTwo: UIButton!
    @IBOutlet weak var buttonThree: UIButton!
    @IBOutlet weak var buttonClear: UIButton!
    @IBOutlet weak var buttonFour: UIButton!
    @IBOutlet weak var buttonFive: UIButton!
    @IBOutlet weak var buttonSix: UIButton!
    @IBOutlet weak var buttonX: UIButton!
    @IBOutlet weak var buttonSeven: UIButton!
    @IBOutlet weak var buttonEight: UIButton!
    @IBOutlet weak var buttonNine: UIButton!
    @IBOutlet weak var buttonDiv: UIButton!
    @IBOutlet weak var buttonPlus: UIButton!
    @IBOutlet weak var buttonZero: UIButton!
    @IBOutlet weak var buttonMin: UIButton!
    @IBOutlet weak var buttonEq: UIButton!
    @IBOutlet weak var buttonSet: UIButton!
    var userIsTyping: Bool = false

    required init(coder aDecoder: NSCoder) {
        displayLabel = UILabel()
        buttonOne = UIButton()
        buttonTwo = UIButton()
        buttonThree = UIButton()
        buttonFour = UIButton()
        buttonFive = UIButton()
        buttonSix = UIButton()
        buttonSeven = UIButton()
        buttonEight = UIButton()
        buttonNine = UIButton()
        buttonZero = UIButton()
        buttonClear = UIButton()
        buttonX = UIButton()
        buttonPlus = UIButton()
        buttonDiv = UIButton()
        buttonMin = UIButton()
        buttonEq = UIButton()
        buttonSet = UIButton()
        super.init(coder: aDecoder)
    }

1 Answer 1

1

From Apple documentation:

An outlet is a property of an object that references another object. The reference is archived through Interface Builder. The connections between the containing object and its outlets are reestablished every time the containing object is unarchived from its nib file. The containing object holds an outlet declared as a property with the type qualifier of IBOutlet and a weak option.

So, you should not instantiate a @IBOutlet object, since they are meant to be connected to a UI object on storyboard or .nib file.

You've said that you are doing this to avoid nil pointer. When does that happen?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.