我无法解决这个问题。当它尝试将answerCorrightly分配给numberLabel时,我收到运行时错误。 :(
var answeredCorrectly: Int? = 0
func endGame(){
let lastViewLet = LastView()
println("Count of correct answers: \(answeredCorrectly)")
lastViewLet.numberLabel.text = String(stringInterpolationSegment: answeredCorrectly)
answeredCorrectly = 0
performSegueWithIdentifier("toEnd", sender: nil)
}
LastView 类:
import Foundation
import UIKit
class LastView: ViewController {
@IBOutlet weak var numberLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
numberLabel.text = "1"
}
}
最佳答案
您的 answeredCorrectly
var 被赋予了一种可选 int 类型,但您为其分配了 0 值。你确定这就是你想要的吗?如果它始终有一个值,您可以使用 Int
。
如果您使用可选值,则应使用 if let
构造来确保您有一个值:
if let answeredCorrectlyInt = answeredCorrectly as? Int {
println("Count of correct answers: \(answeredCorrectlyInt)")
lastViewLet.numberLabel.text = String(answeredCorrectlyInt)
}
此外,当您可以仅使用 String(answeredCorrectly)
并且它会做正确的事情时,您正在使用 String(stringInterpolationSegment:answeredCorrectly)
。
关于ios - 无法修复错误 : unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31059081/