我遇到组件中所选行的问题我有一个带有2个组件和5行的选择器视图我希望它转换单元。麻烦的是,当我尝试向下滚动开关语句不起作用。唯一有效的是var秒。如果我尝试Hectare - >正方形英寸它正确转换它,但如果我尝试英亩 - >平方英寸它给我与公顷相同的答案 - >方英寸。此外,当我添加尝试添加另一个selectedRowInComponent(2)时,应用程序崩溃。
Acre Acre
Hectare Hectare
Square Inch Square Inch
Square Meter Square Meter
Square Kilometer Square Kilometer
Square Foot Square Foot
这是我正在使用的代码
var one: NSInteger = onlyPicker.selectedRowInComponent(0)
var second: NSInteger = onlyPicker.selectedRowInComponent(1)
switch (one) {
case 0:
answerField2.text = answerField1.text
case 1:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 2.4711
var answer = num1 / num2
var display = answer.description
answerField2.text = display
case 2:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 6272600
var answer = num1 * num2
var display = answer.description
answerField2.text = display
case 3:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 0.00023711
var answer = num1 / num2
var display = answer.description
answerField2.text = display
case 4:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 247.11
var answer = num1 / num2
var display = answer.description
answerField2.text = display
case 5:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 43560
var answer = num1 * num2
var display = answer.description
answerField2.text = display
default:
println("not a value")
}
switch (second) {
case 0:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 2.4711
var answer = num1 * num2
var display = answer.description
answerField2.text = display
case 1:
answerField2.text = answerField1.text
case 2:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 15500000
var answer = num1 / num2
var display = answer.description
answerField2.text = display
case 3:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 0.00010000
var answer = num1 / num2
var display = answer.description
answerField2.text = display
case 4:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 100
var answer = num1 / num2
var display = answer.description
answerField2.text = display
case 5:
var num1 = (answerField1.text as NSString).floatValue
var num2:Float = 107640
var answer = num1 * num2
var display = answer.description
answerField2.text = display
default:
println("not a value")
}
}
}
答案 0 :(得分:0)
在崩溃问题上,如果它崩溃了这样的代码,那么请检查UIPicker上的numberOfComponents属性。如果它被设置为2 - 则那就是问题。
var third: NSInteger = onlyPicker.selectedRowInComponent(2)
在计算中,如下重新分解代码将消除switch语句。转换变量是一个Float类型的数组数组(使用新的Swift样式 - 从Xcode 6 Beta 3开始)。
我列出了转换值的名称,以说明如何设置/填充数组。这些应该用Float值替换。
var conversions: [[Float]] = [ [acre-to-acre,acre-to-hectace,acre-to-square-inch,....],
[hectacre-to-acre,hectacre-tohectacre,hectacre-to-square-inch,...],
[square-inch-to-acre,square-inch-to-hectacre,...]
....
]
var one: NSInteger = onlyPicker.selectedRowInComponent(0)
var second: NSInteger = onlyPicker.selectedRowInComponent(1)
var num1 = (answerField1.text as NSString).floatValue
var conversionFactor:Float = conversions[one][two]
var answer = num1 * conversionFactor
var display = answer.description
answerField2.text = display