为什么这行
first = Double(label.text)!
给出此错误
表达式类型'@lvalue String?'没有更多上下文就变得模棱两可
答案 0 :(得分:1)
label.text
是可选的。改用它:
let label = UILabel()
label.text = "12"
guard let text = label.text else {
fatalError("No text")
}
guard let first = Double(text) else {
fatalError("The text isn't a double")
}
print(first) //12.0
答案 1 :(得分:0)
只需简单地
let text = label.text ?? "3.0"
let first = Double(text)
print(first)