binding - 无法转换“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 中收到此错误。我想我试图以错误的方式获取数据。
解决方案
我认为这里没有绑定的必要性,所以只需将相应的地方替换为 simple Int
:
struct CircularProgress: View {
var steps: Int
和
struct Outline: View {
var steps: Int
推荐阅读
- android - Cordova Emulate Android 找不到 Gradle 或安装的 Android Studio,即使它在那里
- postgresql - 使用 postgreSQL 从多个服务器查询表
- java - MYSQL/JDBC 形成一个 JDBC 连接 URL 以连接到名称中带有问号 (?) 的数据库?
- c++ - OSG 地球相机位置
- angular - 如何在 [ngClass] 中混合条件和串联
- parsing - 使用 YamlDotNet 时可以在文件中包含空行吗
- python - 如何禁用 Facebook Messenger 持久菜单?
- javascript - 存储迭代器的所有值
- r - 以特定顺序连接两个字符串向量
- python - 使用python将excel文件转换为文本文件时删除索引值