最近学习swift,使用@state 和@Binding View_Previews预览时出现如下错误
import SwiftUI
@main
struct WebApp: App {
@State var loginflag = true
var body: some Scene {
WindowGroup {
if loginflag == true {
LoginView(loginflag: self.$loginflag)
}
else {
MainUIView()
}
}
}
}
import SwiftUI
struct LoginView: View {
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView(loginflag : true) //这里报错Cannot convert value of type 'Bool' to expected argument type 'Binding<Bool>
}
}
解决问题:
LoginView(loginflag : .constant(true))