gpt4 book ai didi

ios - 无法将类型 'Published.Publisher' 的值转换为预期的参数类型 'Binding'

转载 作者:行者123 更新时间:2023-12-01 15:35:42 28 4
gpt4 key购买 nike

尝试编译以下代码时:

class LoginViewModel: ObservableObject, Identifiable {
@Published var mailAdress: String = ""
@Published var password: String = ""
@Published var showRegister = false
@Published var showPasswordReset = false

private let applicationStore: ApplicationStore

init(applicationStore: ApplicationStore) {
self.applicationStore = applicationStore
}

var passwordResetView: some View {
PasswordResetView(isPresented: $showPasswordReset) // This is where the error happens
}
}
PasswordResetView 如下所示:
struct PasswordResetView: View {
@Binding var isPresented: Bool
@State var mailAddress: String = ""

var body: some View {
EmptyView()
}
}
}
我收到错误编译错误
Cannot convert value of type 'Published<Bool>.Publisher' to expected argument type 'Binding<Bool>'
如果我从 LoginViewModel 类之外使用发布的变量,它就可以正常工作:
struct LoginView: View {
@ObservedObject var viewModel: LoginViewModel

init(viewModel: LoginViewModel) {
self.viewModel = viewModel
}

var body: some View {
PasswordResetView(isPresented: self.$viewModel.showPasswordReset)
}
}
有什么建议我在这里做错了吗?我有没有机会从所属类内部将发布的变量作为绑定(bind)传递?
谢谢!

最佳答案

当有一个非常直接的解决方案时,不知道为什么这里提出的解决方案如此复杂。
similar Reddit question 上找到了这个答案:

The problem is that you are accessing the projected value of an @Published (which is a Publisher) when you should instead be accessing the projected value of an @ObservedObject (which is a Binding), that is: you have globalSettings.$tutorialView where you should have $globalSettings.tutorialView.

关于ios - 无法将类型 'Published<Bool>.Publisher' 的值转换为预期的参数类型 'Binding<Bool>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63282377/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com