错误:二进制运算符'=='无法应用于类型'UILabel?'的操作数?和'String'
import UIKit
class ViewController: UIViewController {
let Soft = 5
let Medium = 8
let Hard = 12
@IBAction func hardnessSelected(_ sender: UIButton) {
let hardness = sender.titleLabel
if hardness == "Soft"{
print(Soft)
}
else if hardness == "Medium"{
print (Medium)
}
else {
print (Hard)
}
}
}
我该如何解决此错误?
让硬度= sender.titleLabel.text
您正在尝试比较两个不同的对象。要获取UILabel的实际文本,您需要hardness.text
。