1

I use this struct :

struct Constants {
  struct array {
     static let fuel = [NSLocalizedString("Gasoline", comment: ""),
                           NSLocalizedString("Diesel", comment: ""),
                           NSLocalizedString("Hybrid", comment: ""),
                           NSLocalizedString("Electric", comment: ""),
                           NSLocalizedString("other", comment: "")]
  }
}

I do the call Constants.array.fuel in other place, it's work fine. the problem is when I change the app language, the NSLocalizedString not working as expected (I get the old translate). probably because I use static. in other viewcontrollers, NSLocalizedString works fine. when I remove static, I get :

Instance member 'fuel' cannot be used on type 'Constants.array'

any help please.

1 Answer 1

4

The problem is fuel property is a constant. Its initialized only once and then won't change during whole application lifetime.

You may make it calculated property by replacing static let with static var fuel: [NSLocalizedString] { return [NSLocalizedString("Gasoline", comment: ""), ...] }

This way, property will be calculated each time you access it. Of course, it won't work as fast as with constant.

Sign up to request clarification or add additional context in comments.

2 Comments

as you said it won't work as fast as with constant. is there any other way to reset the struct for exemple? otherwise I'll accept your answer. and thank you
@abdel You can make a private optional property which you set to nil. Then make your property a computed property, which if the private property is nil sets it to your array and then returns the private property unwrapped. When you change the language, just set the private property to nil again to force it to be recalculated on next use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.