UserDefaults inside a generic Struct is Slow?

Following is a piece of code which I am trying to implement.

struct GetDefaultsValueFromLocalStruct {
  static let userName: String = {
     var userNameToReturn: String = ""
     if let userNameCheck = UserDefaults.standard.object(forKey: "userName") as? String,
        userNameToReturn = userNameCheck
    } else {
    return userNameToReturn

Here GetDefaultsValueFromLocalStruct is a generic struct which can be accessable from anywhere in the project. Now “userName” is the key for storing the user name from server/user entered value into the UserDefaults. I am surely updating “userName” key when I am getting the user name.

On another hand I am using these struct and the object as follows –

if !GetDefaultsValueFromLocalStruct.userPhoneNumber.isEmpty {
 //Do some check here   

Surely, I have an another function inside a struct which actually cleans the data from the UserDefaults when the app is being logged out. Which is as follows –

struct DeleteDefaultsValueFromLocalStruct {
 static func removeObjectsFromDefaults(){
    UserDefaults.standard.removeObject(forKey: "userName")

These can be called as usually –


Now the problem is when I am logging out from the app data must clear from UserDefaults which in these case removeObjectsFromDefaults() will do I guess. Upon logging in to the app again the first method e.g. – GetDefaultsValueFromLocalStruct.userName is giving me the old value which was stored previously.

Surprisingly if I call following code instead !GetDefaultsValueFromLocalStruct.userPhoneNumber.isEmpty giving the current stored value-

if let userNameCheck = UserDefaults.standard.object(forKey: "userName") as? String,
  //Some condition goes here

Is there any problem in my code or does it behaves like that way? Suggestions and improvements are most welcome.