在 prepareForSegue() 中崩溃
Crash in prepareForSegue()
一旦时间 运行 结束,我正在尝试将游戏的得分转移到 viewController。我目前将这段代码合二为一 viewController.
var score = 0
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toResults" {
var svc = segue.destinationViewController as! Results
svc.resultsScore == (score)
}
}
在我的第二个视图控制器中,我有以下代码:
var resultsScore: Int!
@IBOutlet weak var finalScore: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
finalScore.text = "\(resultsScore)"
}
在第一个 viewController 中,当尝试将分数传递给 Results viewController 中的 resultsScore 时,它在这一行崩溃并显示此错误代码。
svc.resultsScore == (score)
fatal error: unexpectedly found nil while unwrapping an Optional value
为什么会这样?我之前已经将数据从一个 VC 传递到另一个,但这并没有发生。以这种方式传递它不安全吗?
svc.resultsScore == (score)
== 是比较运算符。
用1个等号赋值,=
svc.resultsScore = (score)
一旦时间 运行 结束,我正在尝试将游戏的得分转移到 viewController。我目前将这段代码合二为一 viewController.
var score = 0
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toResults" {
var svc = segue.destinationViewController as! Results
svc.resultsScore == (score)
}
}
在我的第二个视图控制器中,我有以下代码:
var resultsScore: Int!
@IBOutlet weak var finalScore: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
finalScore.text = "\(resultsScore)"
}
在第一个 viewController 中,当尝试将分数传递给 Results viewController 中的 resultsScore 时,它在这一行崩溃并显示此错误代码。
svc.resultsScore == (score)
fatal error: unexpectedly found nil while unwrapping an Optional value
为什么会这样?我之前已经将数据从一个 VC 传递到另一个,但这并没有发生。以这种方式传递它不安全吗?
svc.resultsScore == (score)
== 是比较运算符。
用1个等号赋值,=
svc.resultsScore = (score)