Swiftui Optionale Zustandsvariable -Bindung mit if let
Posted: 01 Mar 2025, 14:17
Der folgende Code erstellt nicht. Wie kann ich es funktionieren? < /P>
Code: Select all
import SwiftUI
struct TestView: View {
@State var model:TestModel?
var body: some View {
if let model = model {
OverlayTestView(textVM: model)
//Error: Cannot convert value of type 'TestModel' to expected argument type 'Binding'
} else {
ProgressView()
.task {
model = TestModel()
}
}
}
}
struct OverlayTestView: View {
@Binding var textVM:TestModel
var body:some View {
Text("Count \(textVM.counter)")
}
}
@Observable
class TestModel {
var counter:Int = 0
}