Value of optional type String? not unwrapped

前端 未结 1 1378
深忆病人
深忆病人 2021-01-11 12:12

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 xCod

1条回答
  •  南方客
    南方客 (楼主)
    2021-01-11 12:20

    Because theButton.titleLabel?.text it not unwrapped.

    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
        }
    }
    

    0 讨论(0)
提交回复
热议问题