【问题标题】:Value of optional type String? not unwrapped可选类型字符串的值?未打开
【发布时间】:2014-09-12 01:47:57
【问题描述】:

我只是无法打开 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
    }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    因为 Button.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
        }
    }
    

    【讨论】:

    • 我知道我们不允许(有点)说谢谢,但是谢谢祖园!像魅力一样工作!!! :)
    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多