3

As the @AppStorage and @SceneStorage property wrappers are only available in Views, this means I cannot specify them in my helper functions.

For instance a helper function convertFromKelvin(degreesK: Double) -> Double may want to look up whether to return degrees Fahrenheit or Celsius, as defined in a variable attached to a View such as @AppStorage("tempScale") var tempScale = "C".

It seems logical to go the "old way" for this and reference NSUserDefaults.standard.string(forKey: "tempScale") but is that "right" and how do I reference a @SceneStorage variable? Is it sensible to pass the value on invocation of the function in the View? Or to pass a reference to the Scene, and if so, how would I do that?

It seems counterintuitive to have to pass anything around like that when the values are stored globally, or semi-globally in the Scene context.

Note that the use case I have requires the helper function to be available to multiple unrelated Views.

zkarj
  • 657
  • 9
  • 23
  • You can pass those values in function arguments in place of call. Without code it hard to say something more definite. Would you show your code? – Asperi Aug 27 '20 at 04:47
  • @Asperi my code is a bit complex to post as is. I will see if I can find time to come up with a simplified example. I have considered passing in the value because it is easily accessible from any view simply by re-declaring it, but in my case I have several helper functions that call each other and so they have to pass it around amongst themselves too. It's not hard, it just seems wrong when the wrapper is supposed to avoid this kind of thing. – zkarj Aug 27 '20 at 06:53

0 Answers0