gpt4 book ai didi

ios - init 函数中出现“预期声明”错误(swift)

转载 作者:行者123 更新时间:2023-11-29 02:16:44 25 4
gpt4 key购买 nike

有人可以帮我弄清楚为什么我无法使用 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)
}

最佳答案

来自 Apple 文档:

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.

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

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

关于ios - init 函数中出现“预期声明”错误(swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com