我正在使用计算器应用程序。我设置了一个输入字段,供用户选择一个值。选择的形式为9英寸,12英寸,18英寸,24英寸等。例如,当它们选择24英寸时,我想在计算中使用.055的值,该值将与计算。一直在搞砸,并且很难过.....在此先感谢!
let myPickerData = [String](arrayLiteral: "9 inches", "12 inches", "14 inches", "16 inches", "18 inches", "24 inches")
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myPickerData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return myPickerData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
rebarspace.text = myPickerData[row]
rebarspace.resignFirstResponder()
}
if rebarspace.text != "9 inches" {
let z = Double (0.1466)
}
if rebarspace.text != "12 inches" {
let z = Double (0.11)
}
if rebarspace.text != "14 inches" {
let z = Double (0.094)
}
if rebarspace.text != "16 inches" {
let z = Double (0.088)
}
if rebarspace.text != "18 inches" {
let z = Double (0.0733)
}
if rebarspace.text != "24 inches" {
let z = Double (0.0550)
}
let rebar1 = Double(z! * sqft)
let x: String = String(format:"%.2f",rebar1)
totalrebar.text = "Linear Feet of Rebar Needed=\(x)"
我曾尝试使用if let语句将数字分配给pickerview数据,但不断收到警告,然后计算将无法正确运行,所以我知道我缺少了一些内容或方向错误了.....再次感谢
答案 0 :(得分:0)
用类似的短语
if rebarspace.text != "9 inches" {
let z = Double (0.1466)
}
let rebar1 = Double(z! * sqft)
...您声明了z
内侧大括号,因此在外侧大括号中看不到它。因此,当我们到达rebar1
时,不存在z
。
有很多方法可以做到这一点,但是为了简单起见,只是为了让您入门,请像这样重铸它(我假设您的意思是==
而不是!=
):
var z = 0.0
if rebarspace.text == "9 inches" {
z = Double (0.1466)
}
// and so on
let rebar1 = Double(z! * sqft)