ios 初始化前使用的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35836027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variable used before being initialized
提问by Aoi
I m having a problem with this
我有这个问题
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
}
I m having an error in the line
我在行中有错误
destViewController.secondArray = secondArrays.secondTitle
secondArrays used before being initialized
初始化之前使用的 secondArrays
why am I getting that error? Please help, just a newbie with Swift. TIA
为什么我会收到那个错误?请帮助,只是一个 Swift 新手。TIA
回答by Bharat Modi
This is because of the way of initialising variables in Swift, In Swift Class each property need some default value before being used.
这是因为 Swift 中初始化变量的方式,在 Swift 类中,每个属性在使用之前都需要一些默认值。
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.
Swift 中的类初始化是一个两阶段的过程。在第一阶段,每个存储的属性都由引入它的类分配一个初始值。一旦确定了每个存储属性的初始状态,第二阶段就开始了,每个类都有机会在新实例准备好使用之前进一步自定义其存储属性。
Try changing your code as below:
尝试更改您的代码如下:
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
}
}
回答by J.Wang
You'll have the error because secondArrays
could be used without any initialization if your if let
fails.
您将secondArrays
遇到错误,因为如果您if let
失败,可以在没有任何初始化的情况下使用。
Try the following logic:
试试下面的逻辑:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController = segue.destinationViewController as! SecondTableViewController
guard let indexPath = self.tableView.indexPathForSelectedRow else { return }
destViewController.secondArray = secondArrayData[indexPath.row].secondTitle
}
回答by shoe
you can change the type of secondArrays
to an implicitly unwrapped optional (SecondTableData!
) and that should get rid of the build-time error. note if the the variable is not initialized and you try to access it, a runtime error will be thrown
您可以将 的类型更改为secondArrays
隐式解包的可选 ( SecondTableData!
) 并且应该消除构建时错误。请注意,如果变量未初始化并且您尝试访问它,则会引发运行时错误
also, if you change the variable to a let
this method won't work.
此外,如果您将变量更改为 alet
此方法将不起作用。