gpt4 book ai didi

xcode - swift 错误 : Cannot find an initializer for type 'Double' that accepts an argument list of type '(String)'

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:24 25 4
gpt4 key购买 nike

我正在尝试从标记为“temperatureTextField”的文本字段中获取数据并将其分配给“t”,这是一个 Double。理想情况下,用户应该向 temperatureTextField 添加一个数字值。

这是我的方法:

@IBOutlet weak var temperatureTextField: UITextField!

@IBAction func convert(sender: AnyObject) {
let t = Double(temperatureTextField.text!)
let tempM = TemperatureModel(temp: t!)
temperatureTextField.text = String(tempM.toCelsius())
}

红色感叹号来自“let t = Double(temperatureTex...)”这一行

最佳答案

您可能使用的是 Xcode 6,所以 Swift 1.2,但是 Double 的字符串初始值设定项仅在 Swift 2 (Xcode 7) 中可用。

你总是可以使用 NSString 的 doubleValue 属性:

let t = (temperatureTextField.text! as NSString).doubleValue

但我建议尽快使用 Xcode 7 和 Swift 2。

关于xcode - swift 错误 : Cannot find an initializer for type 'Double' that accepts an argument list of type '(String)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766739/

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