'Reset `ObservableObject` properties after logout

I have one ObservableObject class for managing login data:

final class LoginData: ObservableObject {

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

}

Once I logout the app, my firstName, email, mobileNumber are filled up. How can I reset my LoginData properties' value to empty?

enter image description here



Solution 1:[1]

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()
    }
}

Then you can reset the info just by triggering the method:

@ObservedObject var loginData = LoginData()

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

loginData.setInfo(info)
loginData.resetInfo()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1