I am new to swift und swiftUI and started playing around with it. I am getting the following error:
Static method 'buildExpression' requires that 'ValueWidget' conform to 'AccessibilityRotorContent'
.
Screenshot from the error
ContentView.swift
struct ContentView: View {
var body: some View {
let data = (1...100).map { "Item \($0)" }
let columns = [
GridItem(.adaptive(minimum: 80))
]
let v: Double = -10.0
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(data, id: \.self) { item in
ValueWidget(name: item, value: v)
v = v + 0.5;
}
}
.padding(.horizontal)
}
.frame(maxHeight: 300)
}
}
/*
#Preview {
ContentView()
}*/
struct ValueWidget_Previews: PreviewProvider {
static var previews: some View {
ValueWidget(name: "Hello", value: 42.0)
}
}
ValueWidget.swift
import SwiftUI
struct ValueWidget: View {
var name: String
var value: Double
var body: some View {
VStack {
Text(String(format: "%.1f", value))
.font(.system(size: 40))
.fontWeight(.bold)
.foregroundColor(Color.blue)
Text(name)
}
.padding()
.background()
.cornerRadius(10)
.fixedSize()
.frame(width: 120.0, height: 70.0)
.padding()
.accessibilityLabel(Text("\(name), Wert: \(value)"))
}
}
#Preview {
ValueWidget(name: "Hello", value: 42.0)
}
I have tried replacing
#Preview {
ContentView()
}
with
struct ValueWidget_Previews: PreviewProvider {
static var previews: some View {
ValueWidget(name: "Hello", value: 42.0)
}
}
as some research suggested. But this doesn't help.
I am using macOS Sonoma and Xcode 15.0
What is the best way to fix this problem?
Thank you very much
The explanation for why v = v + 0.5 must be omitted is that you cannot perform calculations within a View. These calculations should be factored out and into another file.