You cannot do this if you declare currentUser as let . It should be var instead, or better still private (set) var . Also, you cannot assign currentUser using nil if its type is User (inferred from the way you currently assigned it). Instead, should it be User? .
For example, something like this:
/// ... static private (set) var currentUser: User? = User() static func resetCurrentUser() { currentUser = nil } // ...
private (set) allows you to change the properties only within the current file, and for the rest of the code it will look like let . Then the resetCurrentUser() method can be used to put it in nil .
Or even this:
// ... private static var _currentUser: User? static var currentUser: User { if _currentUser == nil { _currentUser = User() } return _currentUser! } static func resetCurrentUser() { _currentUser = nil } // ...
You can have currentUser as a computed property that guarantees a return value. That way you can reset user nil , yes. But if you later try to read from there, a new instance will be created automatically.
Be careful with multi-threaded access.
source share