init函数中的“预期声明”错误(swift)

时间:2015-02-23 02:44:41

标签: ios xcode swift

有人可以帮我弄清楚为什么我无法使用Xcode6在Swift中修复此错误?我试图初始化我的所有按钮,因此它们并不是零,它告诉我,当我以为我没有宣布某些东西时。有什么建议?必需的init方法行给了我错误。

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 个答案:

答案 0 :(得分:0)

来自Apple documentation

  

出口是引用另一个对象的对象的属性。该引用通过Interface Builder进行归档。每次从包含nib文件取消包含对象时,都会重新建立包含对象与其出口之间的连接。包含对象持有一个声明为属性的出口,其类型限定符为IBOutlet和弱选项。

因此,您不应该实例化@IBOutlet对象,因为它们要连接到storyboard或.nib文件上的UI对象。

您已经说过这样做是为了避免nil指针。什么时候发生?