gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value Computation

转载 作者:行者123 更新时间:2023-11-28 09:10:48 31 4
gpt4 key购买 nike

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var yourScore: UITextField!
@IBOutlet weak var totalScore: UITextField!
@IBOutlet weak var labelText: UILabel!
@IBAction func buttonPressed(sender: AnyObject) {
let score1: Int = yourScore.text.toInt()!
let score2: Int = totalScore.text.toInt()!
let mistakes = score2 - score1
let scoreFinal = ((((score2 / 2) - mistakes)*23)/score2)+75
labelText.text = "\(scoreFinal)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

我的代码有问题。是因为数据类型还是什么?当我加载应用程序时,它可以正常打开,但是当按下按钮时,应用程序崩溃了。

http://i.stack.imgur.com/B2i5Z.png

最佳答案

您可以使用 if let 安全地解包值:

if let score1 = yourScore.text.toInt() {
if let score2 = totalScore.text.toInt() {
let mistakes = score2 - score1
let scoreFinal = ((((score2 / 2) - mistakes)*23)/score2)+75
labelText.text = "\(scoreFinal)"
} else {
// no valid score2
}
} else {
// no valid score1
}

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value Computation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431694/

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