初始化程序 'xxx' 要求 'Int' 符合 'BinaryFloatingPoint'

Initializer 'xxx' requires that 'Int' conform to 'BinaryFloatingPoint'

我正在制作一个基于视图模型的滑块,但我遇到了这个错误消息Initializer 'init(value:in:step:label:minimumValueLabel:maximumValueLabel:onEditingChanged:)' requires that 'Int.Stride' (aka 'Int') conform to 'BinaryFloatingPoint'

这很奇怪,因为将整数从视图模型转换为 Double 并不能完全解决问题。

我发现了非常相似的问题并阅读了 SO 答案 (),但似乎我无法为我的案例实施解决方案,可能是因为我正在使用 ObservedObject视图模型。

如果我删除 setInformationVM.elapsedRestTime 前面的 $,我会看到另一条错误消息说 Cannot convert value of type 'Int' to expected argument type 'Binding<Int>'

他们说“绑定通常在需要双向通信时使用”- 这是否意味着 Slider 需要一种方法 communicate/update 回到视图模型? 为什么 Slider 一般接受 @State private var xx: Double 作为值,而不是我的视图模型中的简单整数?

import Foundation
import SwiftUI
import Combine

struct SetRestDetailView: View {
    
    @EnvironmentObject var watchDayProgramVM: WatchDayProgramViewModel

    @State var showingLog = false

    var body: some View {

            GeometryReader { geometry in

             ZStack() {
                    (view content removed for readability)
              }

        .sheet(isPresented: $showingLog) {
            
            let setInformatationVM = self.watchDayProgramVM.exerciseVMList[0].sets[2]
            
            setLoggingView(setInformationVM: setInformatationVM, restfullness: 3, stepValue: 10)
        }
        
    }
}

setLoggingView

struct setLoggingView: View {
    
    @Environment(\.dismiss) var dismiss
    
    @ObservedObject var setInformationVM: SetInformationTestClass
    @State var restfullness: Int
    var stepValue: Int
    
    var body: some View {
        
        GeometryReader { geometry in
            
            let rect = geometry.frame(in: .global)
            
            ScrollView {
                
                VStack(spacing: 5) {
                    
                    Text("Rested  \(Int(setInformationVM.elapsedRestTime)) sec")
                    
                    Slider(value: $setInformationVM.elapsedRestTime,
                           in: 0...setInformationVM.totalRestTime,
                           step: Int.Stride(stepValue),
                           label: {
                        Text("Slider")
                    }, minimumValueLabel: {
                        Text("-\(stepValue)")
                    }, maximumValueLabel: {
                        Text("+\(stepValue)")
                    })
                        .tint(Color.white)
                        .padding(.bottom)

                    Divider()

                    Spacer()
                    
                    Text("Restfullness")
                        .frame(minWidth: 0, maxWidth: .infinity)
                    
                    restfullnessStepper(rect: rect, maxRestFullness: 5, minRestFullness: 1, restfullnessIndex: restfullness)
                
                    Button(action: {
                        print("Update Button Pressed")
                        
                        //TODO
                        //perform further actions to update restfullness metric and elapsed rest time in the viewmodels before dismissing the view, and also update the iOS app by synching the view model.
                        
                        dismiss()
                        
                    }) {
                        HStack {
                            Text("Update")
                                .fontWeight(.medium)
                        }
                    }
                    .cornerRadius(40)
                    
                }
                .border(Color.yellow)
            }
        }
    }

SetInformationTestClass 视图模型

class SetInformationTestClass: ObservableObject {
    
    init(totalRestTime: Int, elapsedRestTime: Int, remainingRestTime: Int, isTimerRunning: Bool) {
        
        self.totalRestTime = totalRestTime
        self.elapsedRestTime = elapsedRestTime
        self.remainingRestTime = remainingRestTime
        
    }
    
    @Published var totalRestTime: Int
    @Published var elapsedRestTime: Int
    @Published var remainingRestTime: Int
    
    

您可以创建自定义绑定变量,例如:

let elapsedTime = Binding(
            get: { Double(self.setInformationVM.elapsedRestTime) },
            set: { self.setInformationVM.elapsedRestTime = Int([=10=]) } // Or other custom logic
        )

// then you reference it in the slider like:

Slider(elapsedTime, ...)