我只是无法打开包装 else
块。 xCode给了我“修复它!和??”的选项,遗憾的是它也没有解决问题。
我在xCode中收到此错误:
可选类型'String?'的值没有打开;你的意思是用!要么 ??
@IBAction func buttonTapped(theButton: UIButton) {
if answerField.text == "0" {
answerField.text = theButton.titleLabel?.text
} else {
answerField.text = answerField.text + theButton.titleLabel?.text
}
因为theButton.titleLabel?.text它没有打开。
您可以使用
answerField.text = answerField.text + (theButton.titleLabel?.text ?? "")
要么
if answerField.text == "0" {
answerField.text = theButton.titleLabel?.text
} else {
if let txt = theButton.titleLabel?.text {
answerField.text = answerField.text + txt
} else {
answerField.text = answerField.text
}
}
因为theButton.titleLabel?.text它没有打开。
您可以使用
answerField.text = answerField.text + (theButton.titleLabel?.text ?? "")
要么
if answerField.text == "0" {
answerField.text = theButton.titleLabel?.text
} else {
if let txt = theButton.titleLabel?.text {
answerField.text = answerField.text + txt
} else {
answerField.text = answerField.text
}
}