ios 可选类型字符串的值?没有打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25799529/
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
Value of optional type String? not unwrapped
提问by Insane
I am just not able to unwrap else
block. xCode gives me options to "Fix it with ! and ??", which sadly does not fix the issue either.
I get this error in xCode:
Value of optional type 'String?' not unwrapped; did you mean to use ! or ??
我只是无法解开else
块。xCode 为我提供了“使用 ! 和 ?? 修复它”的选项,但遗憾的是,这也不能解决问题。我在 xCode 中收到此错误:可选类型“字符串?”的值 未解开;你是想用吗!或者 ??
@IBAction func buttonTapped(theButton: UIButton) {
if answerField.text == "0" {
answerField.text = theButton.titleLabel?.text
} else {
answerField.text = answerField.text + theButton.titleLabel?.text
}
回答by ZYiOS
Because theButton.titleLabel?.text it not unwrapped.
因为 Button.titleLabel?.text 它没有解开。
You can use
您可以使用
answerField.text = answerField.text + (theButton.titleLabel?.text ?? "")
or
或者
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
}
}