错误:“在声明前无法捕获“单词”” iOS swift

时间:2019-06-05 13:23:50

标签: swift

我正在使用splitviewcontroller为字典应用程序编写代码。我为“ Word”类设置了各种条目,当我尝试为它们添加标签时,计算机现在不会读取这些条目。

导入UIKit

类DetailViewController:UIViewController {

class Word {
let name: String
let meaning: String
let numberOfTimesTapped: String

init(name: String, meaning: String, numberOfTimesTapped: String) {
    self.name = name
    self.meaning = meaning
    self.numberOfTimesTapped = numberOfTimesTapped
}

}


@IBOutlet weak var WordLabel: UILabel!
@IBOutlet weak var DescriptionLabel: UILabel!
@IBOutlet weak var NumberOfTimesTappedLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    func refreshUI() {
        loadViewIfNeeded()
WordLabel.text = word?.name //[THIS IS WHERE I GET THE ERROR: "Cannot capture 'word' before it is declared" ALTHOUGH IT'S BEEN CLEARLY DECLARED BEFORE!!!]
DescriptionLabel.text = word?.meaning
NumberOfTimesTappedLabel.text = word?.numberOfTimesTapped
    }


    var word: Word? {
        didSet {
            refreshUI()

        }
    }

1 个答案:

答案 0 :(得分:2)

如果使用适当的缩进,您会发现它容易得多,但是您在var word的{​​{1}} inside 中声明。您需要在其外部声明它,以便可以在refreshUI内部访问范围。另外,您在refreshUI的{​​strong>内部中声明refreshUI,这很可能不是您想要的。该代码的固定版本为

viewDidLoad