无法转换“ Int”类型的值?到预期的参数类型'Binding <Int>'SwiftUI

时间:2020-11-06 09:32:37

标签: binding swiftui

我创建了一个循环进度视图,以便能够根据步骤数据显示进度条。但是由于某些原因,我无法访问stepView文件中的step.count。

这是我的StepView

    struct StepView: View {
        private var healthStore: HealthStore?
        @State private var presentClipboardView = true
        @State private var steps: [Step] = [Step]()
        init() {
            healthStore = HealthStore()
        }
        private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
            let now = Date()
            let startOfDay = Calendar.current.startOfDay(for: now)
            statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
                let count = statistics.sumQuantity()?.doubleValue(for: .count())
                let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
                steps.append(step)
            }
        }
        var body: some View {  
            VStack {
                ForEach(steps, id: \.id) { step in
                    VStack {
                            HStack{
                                Text("WC")
                                Text("\(step.wc)")
                            }
                            HStack {
                                Text("\(step.count ?? 0)")
                                Text("Total Steps")
                            }
                            Text(step.date, style: .date)
                                .opacity(0.5)
                        CircularProgress(steps: step.count) //ERROR
                            Spacer()
                    }
                }
                .navigationBarBackButtonHidden(true)
                
            }
            
            .onAppear() {
                if let healthStore = healthStore {
                    healthStore.requestAuthorization { (success) in
                        if success {
                            healthStore.calculateSteps { (statisticsCollection) in
                                if let statisticsCollection = statisticsCollection {
                                    updateUIFromStatistics(statisticsCollection)
                                }
                            }
                        }
                    }
                }
            }
            .onDisappear() {
                self.presentClipboardView.toggle()
            }
        }
    }

这是我的循环进度视图

    struct CircularProgress: View {
    var steps: Binding<Int>
    var body: some View {
        ZStack {
            Color.progressBarColor
                .edgesIgnoringSafeArea(.all)
            VStack {
                ZStack {
                    Label()
                    Outline(steps: steps)
                }
            }
        }
    }
}

struct Label: View {
    var percentage: CGFloat = 20
    var body : some View {
        ZStack {
            Text(String(format: "%.0f", percentage))
                .font(Font.custom("SFCompactDisplay-Bold", size: 56))
        }
    }
}

struct Outline: View {
    var steps: Binding<Int>
    var percentage: CGFloat = 20
    var colors : [Color] = [Color.trackProgressBarColor]
    var body: some View {
        ZStack {
            Circle()
                .fill(Color.clear)
                .frame(width: 250, height: 250)
                .overlay(
                    Circle()
                        .trim(from: 0, to: percentage * 0.01)
                        .stroke(style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .round))
                        .fill(AngularGradient(gradient: .init(colors: colors), center: .center, startAngle: .zero, endAngle: .init(degrees: 360)))
                ).animation(.spring(response: 2.0, dampingFraction: 1.0, blendDuration: 1.0))
        }
    }
}

在步骤视图中调用循环过程时,我在步骤视图中遇到此错误。我想我试图以错误的方式获取数据。

1 个答案:

答案 0 :(得分:1)

我认为这里没有必要绑定,因此只需用简单的Int替换相应的位置即可:

struct CircularProgress: View {
  var steps: Int

struct Outline: View {
    var steps: Int