我是SwiftUI的新手,我需要社区的帮助来理解为什么在下面的代码中,任何选择器选项的选择似乎都不起作用,要么是不可选择的,因此不会触发任何操作,要么是被选择的,并且不会触发任何操作。
非常感谢提前让我知道下划线的概念。
德沃
import SwiftUI
struct ContentView: View {
@State private var measureToConvert = ""
@State private var conversionFromMeasure = "km"
@State private var convertedToMeasure = "m"
let measure = ["m", "km", "ft", "yd", "mi"]
func convertInMillimeters(input: String, number: String) -> Double {
var value: Double = 0
if let realNumber = Double(number) {
switch input {
case "m":
let conversion = (Double(realNumber) * 1_000)
value = conversion
case "km":
let conversion = (Double(realNumber) * 1_000_000)
value = conversion
case "ft":
let conversion = (Double(realNumber) * 304.8)
value = conversion
case "yd":
let conversion = (Double(realNumber) * 914.4)
return conversion
case "mi":
let conversion = (Double(realNumber) * 1_609_344)
value = conversion
default:
let conversion = -2.0
value = conversion
}
}
print("value in millimeters is \(value)")
return value
}
func convertToNewMeasure(input: String, number: Double) -> Double {
var value: Double = 0
switch input {
case "m" :
let conversion = (number / 1_000)
value = conversion
case "km":
let conversion = (number / 1_000_000)
value = conversion
case "ft":
let conversion = (number / 304.8)
value = conversion
case "yd":
let conversion = (number / 914.4)
return conversion
case "mi":
let conversion = (number / 1_609_344)
value = conversion
default:
let conversion = -1.0
value = conversion
}
print("value converted is \(value)")
return value
}
var inMillimeters : Double {
return convertInMillimeters(input: conversionFromMeasure, number: measureToConvert)
}
var result: Double {
return convertToNewMeasure(input: convertedToMeasure, number: inMillimeters)
}
var body: some View {
NavigationView{
Form{
Section(header: Text("Value to convert:")){
TextField("Enter a measure", text: $measureToConvert).keyboardType(.decimalPad)
}
Section(header: Text("Select input measure")){
Picker("From", selection: $conversionFromMeasure) {
ForEach(0 ..< measure.count){
Text("\(self.measure[$0])")
}
}.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Select output measure")){
Picker("To", selection: $convertedToMeasure) {
ForEach(0 ..< measure.count) {
Text("\(self.measure[$0])")
}
}.pickerStyle(SegmentedPickerStyle()) }
Section(header: Text("Converted value")){
Text("\(result, specifier: "%.2f")")
}
}
.navigationBarTitle("Measure Converter")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
当使用一个具有这几个选项的选择器时,使用枚举来存储值是非常方便的,您也可以使用枚举来存储函数并返回您想要的结果。您可以在这里了解有关枚举的更多信息
一旦您创建了枚举,然后使它符合CaseIterable,这将为您提供一个包含所有案例的数组,然后您将在foreach中使用这些案例。
您还给它一个rawValue,它将作为填充每个选择器选项的内容。很清楚我在代码中做了什么。
import SwiftUI
struct ContentView: View {
enum Unit: String, CaseIterable {
case meters = "m"
case kilometers = "km"
case feet = "ft"
case yards = "yd"
case miles = "mi"
}
@State private var unitSelection: Unit = .meters
@State private var measureToConvert = ""
@State private var conversionFromMeasure = "km"
@State private var convertedToMeasure = "m"
let measure = ["m", "km", "ft", "yd", "mi"]
func convertInMillimeters(input: String, number: String) -> Double {
var value: Double = 0
if let realNumber = Double(number) {
switch input {
case "m":
let conversion = (Double(realNumber) * 1_000)
value = conversion
case "km":
let conversion = (Double(realNumber) * 1_000_000)
value = conversion
case "ft":
let conversion = (Double(realNumber) * 304.8)
value = conversion
case "yd":
let conversion = (Double(realNumber) * 914.4)
return conversion
case "mi":
let conversion = (Double(realNumber) * 1_609_344)
value = conversion
default:
let conversion = -2.0
value = conversion
}
}
print("value in millimeters is \(value)")
return value
}
func convertToNewMeasure(input: String, number: Double) -> Double {
var value: Double = 0
switch input {
case "m" :
let conversion = (number / 1_000)
value = conversion
case "km":
let conversion = (number / 1_000_000)
value = conversion
case "ft":
let conversion = (number / 304.8)
value = conversion
case "yd":
let conversion = (number / 914.4)
return conversion
case "mi":
let conversion = (number / 1_609_344)
value = conversion
default:
let conversion = -1.0
value = conversion
}
print("value converted is \(value)")
return value
}
var inMillimeters : Double {
return convertInMillimeters(input: conversionFromMeasure, number: measureToConvert)
}
var result: Double {
return convertToNewMeasure(input: convertedToMeasure, number: inMillimeters)
}
var body: some View {
NavigationView{
Form{
Section(header: Text("Value to convert:")){
TextField("Enter a measure", text: $measureToConvert).keyboardType(.decimalPad)
}
Section(header: Text("Select input measure")){
Picker("From", selection: $unitSelection) {
ForEach(Unit.allCases, id: \.self){ unit in
Text(unit.rawValue)
}
}.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Select output measure")){
Picker("To", selection: $convertedToMeasure) {
ForEach(0 ..< measure.count) {
Text("\(self.measure[$0])")
}
}.pickerStyle(SegmentedPickerStyle()) }
Section(header: Text("Converted value")){
Text("\(result, specifier: "%.2f")")
}
}
.navigationBarTitle("Measure Converter")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
您可以通过更改以下内容来使选择器工作:
ForEach(0 ..< measure.count){
Text("\(self.measure[$0])")
}
对此:
ForEach(measure, id: \.self) {
Text($0)
}
所以这两个部分应该是:
Section(header: Text("Select input measure")){
Picker("From", selection: $conversionFromMeasure) {
ForEach(measure, id: \.self) {
Text($0)
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Select output measure")){
Picker("To", selection: $convertedToMeasure) {
ForEach(measure, id: \.self) {
Text($0)
}
}.pickerStyle(SegmentedPickerStyle())
}