Sujal
Sujal

Reputation: 1527

Clearing static variables in a struct/ class

I have a struct with some static fields as below.

struct User {
    static var userId: String?
    static var email: String?
    static var phone: String? 
}

When the user logs in, these variables are set and used in other view controllers. When the user logs out, I would like to clear all those details. Is there a quick way to clear or should I individually clear them like

User.userId = ""
User.email = ""
User.phone = ""

Upvotes: 1

Views: 608

Answers (2)

Lësha Turkowski
Lësha Turkowski

Reputation: 1391

Instead of having static variables you could have an optional static shared User variable and set it to nil when user is logged out:

struct User {

    static var shared: User?

    var userId: String?
    var email: String?
    var phone: String?
}

User.shared = nil // user logged out
User.shared = User(userId: ..., email: ..., phone: ...) // user logged in

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100533

You can set a function in the struct to do that or better to set user to nil and check for nil logic in your app

Upvotes: 1

Related Questions