gpt4 book ai didi

ios - 开启 UIButton title : Expression pattern of type 'String' cannot match values of type 'String?!'

转载 作者:IT王子 更新时间:2023-10-29 05:41:05 35 4
gpt4 key购买 nike

我正在尝试在 @IBAction 方法中使用一个开关,该方法连接到多个按钮

@IBAction func buttonClick(sender: AnyObject) {

switch sender.currentTitle {
case "Button1":
print("Clicked Button1")
case "Button2":
print("Clicked Button2")
default:
break
}

当我尝试上述操作时,出现以下错误:

Expression pattern of type 'String' cannot match values of type 'String?!'

最佳答案

currentTitle 是可选的,因此您需要将其解包。此外,sender 的类型应该是 UIButton,因为您正在访问 currentTitle 属性。

@IBAction func buttonClick(sender: UIButton) {
if let theTitle = sender.currentTitle {
switch theTitle {
case "Button1":
print("Clicked Button1")
case "Button2":
print("Clicked Button2")
default:
break
}
}
}

关于ios - 开启 UIButton title : Expression pattern of type 'String' cannot match values of type 'String?!' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814250/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com