Home > Mobile >  How to reset static variables in a struct to their default values - Swift
How to reset static variables in a struct to their default values - Swift

Time:11-24

I have a struct with some static variables that I need to use throughout my app (I didn't use a singleton class yet because I may need multiple instances in the future, but right now I just have this single instance).

Each static variable is assigned a default value, but these values get changed throughout the lifetime of the app. I also store these changes in UserDefaults so they would be persistent between invocations, but I still use these static variables because I don't want to read from the UserDefaults every time.

Now I want the option to reset all these values to their original default values and I can't figure out how to do this correctly. I can use a separate struct which will hold the default values and then use a func to change the values from one to its respective variable in the other. But it seems not so elegant and there must be a nicer way to do this that I'm missing.

I tried to add a func inside the struct which does the following:

func resetDefaults() {
    self = DefaultStruct()
}

The compiler told me I can't change self and need to make the func mutating. So I did. But then elsewhere in my code, in another class, this function is no longer in the scope. If I append:

DefaultStruct.resetDefaults()

It gives me other errors, such as:

Instance member 'resetDefaults' cannot be used on type 'DefaultStruct'; did you mean to use a value of this type instead?

Also, because I use the static variables of this struct directly, I never initialized this struct. In other words, I don't have such a struct by name that I can access elsewhere. Or maybe I'm wrong here and I'm missing something???

Your help would be much appreciated. Either I'm missing something very basic or I'll just use a separate struct with the default values as I said before.

Thanks!

CodePudding user response:

As a very trivial example that demonstrates the concept

struct Foo {
   private enum Defaults {
     static let barDefault = "Initial String"
   }
   
   static var bar: String = Defaults.barDefault
   
   static func reset() {
      bar = Defaults.barDefault
   }
   
}
    
print(Foo.bar)  //Initial String

Foo.bar = "New String"
print(Foo.bar)  // New String

Foo.reset()
print(Foo.bar)  // Initial String

This uses a private enum to hold the default strings so it (a) remains inernal to the struct, even when used in by other static attributes and (b) so that it cannot be instantiated by mistake.

If you have a more complex type for the variable, use an initialiser with a default value that is the intial / reset value so you can do something like this:

struct Foo {
   struct ComplexStruct{
       var x: SomeType

       init(x: SomeType = DefaultValue) { self.x = x }
   }

   static var bar = ComplexStruct()
   
   static func reset() {
      bar = ComplexStruct()
   }
}
  • Related