0

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 .

9
  • Your code should work. Could you add print("Settings.shared.lan: \(Settings.shared.lan)") just before MOLH.reset() to display any change. Commented Dec 28, 2022 at 1:40
  • How do you know it doesn't work? Are you sure MOLHLanguage gets updated when you update MOLH? Off topic but I would suggest you create an enum for your languages even if they are only two to make your code cleaner and avoid spelling errors. Commented Dec 28, 2022 at 7:40
  • yes my code is working and language code is changed only inside my function Commented Dec 28, 2022 at 11:46
  • How is MOLHLanguage kept in synch with MOHL? Commented Dec 28, 2022 at 13:55
  • are you saying that the print("Settings.shared.lan: \(Settings.shared.lan)") just beforeMOLH.reset() as I suggested, does not show the change in lan? Commented Dec 28, 2022 at 13:57

1 Answer 1

-1

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.

2
  • I want to understand why lan variable did not change and still hold the initial value "ar" Commented Dec 28, 2022 at 15:30
  • thank you I make didset{} on lan variable and the code working well now.class Settings{ // singelton static let shared = Settings() var lan : String = "en" var defaultLan = "" { didSet{ lan = defaultLan } } private init(){ } } Commented Dec 28, 2022 at 19:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.