gpt4 book ai didi

ios - 初始化前使用的变量

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:40 25 4
gpt4 key购买 nike

我有问题

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController = segue.destinationViewController as! SecondTableViewController
var secondArrays : SecondTableData

if let indexPath = self.tableView.indexPathForSelectedRow {
secondArrays = secondArrayData[indexPath.row]
}
destViewController.secondArray = secondArrays.secondTitle
}

我的行有错误

destViewController.secondArray = secondArrays.secondTitle

secondArrays used before being initialized

为什么我会收到这个错误?请帮助,只是 Swift 的新手。时间差

最佳答案

这是因为在 Swift 中初始化变量的方式,在 Swift Class 中每个属性在使用之前都需要一些默认值。

Class initialisation in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.

尝试如下更改您的代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController = segue.destinationViewController as! SecondTableViewController
var secondArrays : SecondTableData?

if let indexPath = self.tableView.indexPathForSelectedRow {
secondArrays = secondArrayData[indexPath.row]

//Here you're making sure that the secondArrays would must have a value
destViewController.secondArray = secondArrays.secondTitle
}

}

关于ios - 初始化前使用的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35836027/

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