如何在 SwiftUI 中为 Int 类型 属性 创建滑块?

How do I create a slider in SwiftUI for an Int-type property?

我有一个名为“score”的 Int 属性 视图,我想用滑块进行调整。

struct IntSlider: View {
    @State var score:Int = 0

    var body: some View {
        VStack{
            Text(score.description)
            Slider(value: $score, in: 0.0...10.0, step: 1.0)
        }
    }
}

但 SwiftUI 的 Slider 仅适用于 doubles/floats。

如何让它与我的整数一起使用?

struct IntSlider: View {
    @State var score: Int = 0
    var intProxy: Binding<Double>{
        Binding<Double>(get: {
            //returns the score as a Double
            return Double(score)
        }, set: {
            //rounds the double to an Int
            print([=10=].description)
            score = Int([=10=])
        })
    }
    var body: some View {
        VStack{
            Text(score.description)
            Slider(value: intProxy , in: 0.0...10.0, step: 1.0, onEditingChanged: {_ in
                print(score.description)
            })
        }
    }
}