我需要将按钮的文本与变量进行比较。
@IBAction func CheckifATrue(sender: AnyObject) {
if let thisbutton = sender.titleLabel.text {
if thisbutton == theAnswer.text {
score++
println("Correct")}
}
}
给我一个错误,迫使我输入!在标题标签之后。我不能这样做,因为按钮 titleLabel 有可能(实际上是肯定的)为零。为什么 if let 语句不允许我解开包装?是否有另一种方法来拉动按钮文本,我可以通过 if let 安全地删除 optional ,并且仅在有效时执行代码?
最佳答案
您需要将发送者的类型从 AnyObject
更改为 UIButton
,然后您可以通过以下方式解开它:
@IBAction func CheckifATrue(sender: UIButton) {
if let thisbutton = sender.titleLabel?.text {
if thisbutton == theAnswer.text {
score++
println("Correct")}
}
}
关于swift - 展开button.titleLabel.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31623196/