SwiftUI Picker,将String转换为Double

时间:2019-07-10 09:02:02

标签: double picker swiftui

我正在使用SwiftUI,并且有一个Picker在数组中选择一些数字字符串。 选择器工作良好,我可以在屏幕上显示该值,但是我需要能够将数组中选择的字符串设置为Double。

我确定那里有一些非常友善和聪明的人可以为我提供帮助。

我想设置:

var totalFrequency:Double = 0.0

从选择器中的两个值。

谢谢!!!

克雷格

import SwiftUI

struct DipoleSelect : View {
  @State var mhzValueIndex = 0
  @State var mhzValueStepIndex = 0
  var frequencyValue = ["1", "2", "3", "4", "5", "6", "7", "8", "9" ]
  var frequencyStep = [".000", ".005", ".010"]
  var totalFrequency : Double = 0.0

  var body: some View {

    VStack {

      Text("Select Frequency")
        .font(.largeTitle)
        .fontWeight(.heavy)
        .padding([.top,], -180.0)

      Picker(selection: $mhzValueIndex, label: Text("")) {
        ForEach(0 ..< frequencyValue.count) {
          Text(self.frequencyValue[$0] + " mhz").font(.largeTitle).fontWeight(.semibold).color(Color.black).tag($0)
        }.font(.title)

      }.padding(.top, -170)

      Picker(selection: $mhzValueStepIndex, label: Text("")) {
        ForEach(0 ..< frequencyStep.count) {
          Text(self.frequencyStep[$0]).tag($0)
        }

      }.padding()

      Text("You Selected \(frequencyValue[mhzValueIndex])\(frequencyStep[mhzValueStepIndex]) Mhz")

    }

  }

}

1 个答案:

答案 0 :(得分:0)

我想出了这个解决方案。希望它对您有用。

final class Property: BindableObject {
    var didChange = PassthroughSubject<Property, Never>()

    var frequencyValue = ["1", "2", "3", "4", "5", "6", "7", "8", "9" ]
    var frequencyStep = [".000", ".005", ".010"]

    var mhzValueIndex: Int = 0 {
        didSet{
            totalFrequency = totalFrequency + getDouble(value: frequencyValue[mhzValueIndex])
            didChange.send(self)

        }
    }

    var mhzValueStepIndex: Int = 0 {
        didSet {
            totalFrequency = totalFrequency + getDouble(value: frequencyStep[mhzValueStepIndex])
            didChange.send(self)
        }
    }

    var totalFrequency: Double = 0.0 {
        didSet{
            didChange.send(self)
        }
    }

    func getDouble(value: String) -> Double {
        return  Double(value)!
    }
}

struct DipoleSelect : View {

    @EnvironmentObject var property: Property

    var body: some View {

        VStack {

            Text("Select Frequency")
            .font(.largeTitle)
            .fontWeight(.heavy)
            .padding()

            Picker(selection: $property.mhzValueIndex, label: Text("")) {
                ForEach(0 ..< property.frequencyValue.count) {
                    Text(self.property.frequencyValue[$0] + " mhz").font(.largeTitle).fontWeight(.semibold).color(Color.black).tag($0)
                }.font(.title)

            }

            Picker(selection: $property.mhzValueStepIndex, label: Text("")) {
                ForEach(0 ..< property.frequencyStep.count) {
                    Text(self.property.frequencyStep[$0]).tag($0)
                }

            }


            Text("You Selected \(property.frequencyValue[property.mhzValueIndex])\(property.frequencyStep[property.mhzValueStepIndex]) Mhz")


            Text("Total frequency: \(property.totalFrequency)")

       }

    }


}