首页 > 解决方案 > 无法转换“Int”类型的值?到预期的参数类型'绑定' 斯威夫特

问题描述

我创建了一个循环进度视图,以便能够根据步骤数据显示进度条。但由于某种原因,我无法访问 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))
        }
    }
}

在 stepview 中调用 CIRCULARPROGRESS 时,我在 stepview 中收到此错误。我想我试图以错误的方式获取数据。

标签: bindingswiftui

解决方案


我认为这里没有绑定的必要性,所以只需将相应的地方替换为 simple Int

struct CircularProgress: View {
  var steps: Int

struct Outline: View {
    var steps: Int

推荐阅读