注销后重置“ObservableObject”属性

Reset `ObservableObject` properties after logout

我有一个 ObservableObject class 用于管理登录数据:

final class LoginData: ObservableObject {

// MARK: - Properties
@Published var otpVerificationType: OTPVerificationType = .mobileNumber
@Published var firstName: String = ""
@Published var mobileNumber: String = ""
@Published var email: String = ""   

}

一旦我注销该应用程序,我的名字、电子邮件、手机号码就会填满。如何将我的 LoginData 属性的值重置为空?

struct LoginInfo {
    var otpVerificationType: OTPVerificationType
    var firstName: String
    var mobileNumber: String
    var email: String
    
    init(otpVerificationType: OTPVerificationType = .mobileNumber, firstName: String = "", mobileNumber: String = "", email: String = "") {
        self.otpVerificationType = otpVerificationType
        self.firstName = firstName
        self.mobileNumber = mobileNumber
        self.email = email
    }
}

final class LoginData: ObservableObject {
    // MARK: - Properties
    @Published var loginInfo: LoginInfo?

    // MARK: - setInfo
    func setInfo(_ info: LoginInfo) {
        loginInfo = info
    }

    // MARK: - resetInfo
    func resetInfo() {
        loginInfo = LoginInfo()
    }
}

然后你可以通过触发方法重置信息:

@ObservedObject var loginData = LoginData()

let info = LoginInfo(firstName: "John", mobileNumber: "01XXXXXXXXX")

loginData.setInfo(info)
loginData.resetInfo()