How to update variable in Singleton pattern

389 Views Asked by At

I have singleton class has lan variable .it is simple like this

class Settings{
    //  singelton
    static let shared  = Settings()
    var lan:String? = "ar"
      
 
    private init(){
    
    }
 
}

and I have a button which change language code for application from ar to en and when I tabbed it again language code changed from en to ar .

    @IBAction func languageBtnTabbed(_ sender: Any) {
        
        MOLH.setLanguageTo(MOLHLanguage.currentAppleLanguage() == "en" ? "ar" : "en")
        print(MOLHLanguage.currentAppleLanguage())
        Settings.shared.lan = MOLHLanguage.currentAppleLanguage()
        
        MOLH.reset()
       
        
    }

my question is how to update lan variable on setting class with "MOLHLanguage.currentAppleLanguage()"

I tried to update lan variable by using this line : Settings.shared.lan = MOLHLanguage.currentAppleLanguage() but in the fact the lan variable did not change . can any one help me to solve this problem ? and thanks in advance.

i tried to didset{} on lan variable to update it once the language code is changed and the code worked well .

1

There are 1 best solutions below

2
Ayrton CB On

So it might be worth checking what the value of MOLHLanguage.currentAppleLanguage() is, I suspect that the value of MOLHLanguage.currentAppleLanguage() is actually ar perhaps?

try changing the default text for land to something like default and see if value now updates to ar when you run your code.