1

在 iOS 14 之前,我的项目使用以下代码运行良好

struct LoginView: View, Resolving {

    @ObservedObject var loginUIService: LoginUIService = Resolver.resolve()
    ...
    var body: some View {
        ...
        TextField(LocalizedString.Placeholder.email.localized, text: self.$loginUIService.loginViewModel.emailAddress)
        ...
    }
}

这是我的部分服务课程

class LoginUIService: ObservableObject, Resolving {
    
    @Published var loginViewModel: LoginViewModel = Resolver.resolve()
    ...
}

然后是视图模型

class LoginViewModel: ObservableObject {
    
    
    //MARK:- Properties
    @Published var emailAddress: String {
        didSet {
            checkIsValid()
        }
    }
    @Published var password: String{
        didSet {
            checkIsValid()
        }
    }
    @Published var isValid = false

    
    //MARK:- Init Methods
    init() {
        emailAddress = ""
        password = ""
    }
    
    init(emailAddress: String, password: String) {
        self.emailAddress = emailAddress
        self.password = password
    }
    
    fileprivate func checkIsValid() {
        isValid = emailAddress.isEmpty == false && password.isEmpty == false && emailAddress.contains("@")
    }
    
}

在 iOS 13 中,此代码全部 100% 运行,没有任何问题,现在在 iOS14 设备上运行时,我在控制台中点击文本字段时得到以下输出

Binding<String>(transaction: SwiftUI.Transaction(plist: []), location: SwiftUI.LocationBox<SwiftUI.(unknown context at $7fff562e301c).ProjectedLocation<SwiftUI.LocationBox<SwiftUI.ObservableObjectLocation<Recon.LoginUIService, Recon.LoginViewModel>>, Swift.WritableKeyPath<Recon.LoginViewModel, Swift.String>>>, _value: "")

最近有没有人遇到过这个问题?

4

0 回答 0