Learn Courses My Dashboard

Refactor>>>Rename doesnt work with AppStorage variables?

Refactor>>>Renane apparently does not work with AppStorage variables.

The specific failure is that the scope of the rename is limited to varible instances in the file-being-edited only. If you want to rename an AppStorage variable, you must execute the Rename on each file in which the AppStorage variable appears.

This behavior seems ovbiously wrong to me but maybe I don’t understand AppStorage.

I have observed same problem in xcoce 12.5 and xcode 13.

Wherever AppStorage is defined, the var is a unique instance as far as that property Wrapper is concerned. The scope is where that is defined.

If you define it in a ViewModel (conforming to ObservableObject) then you can access it wherever you need to assuming that the view model is relevant to that View. In that case you can refactor it and the change in the var name will ripple through all Views accessing that model property.

Be careful though. Generally the way you name your AppStorage property is such that there is synergy between the parameter and the variable name. ie let’s say you define this AppStorage variable:

@AppStorage("username") var username: String = "annonymous"

If you refactor the var - username - then it wont line up with the parameter name it was given - @AppStoreage("username").

Essentially what you are saving is a key:value pair so the key in the example above is “username” (as defined by the parameter in AppStorage("username") and the value is “anonymous”

If you define @AppStorage("username") var dogsBreakfast: String = "Pal meaty bites" somewhere else with the same parameter name but a different var name then it still references the same parameter value which is - username - so the two locations will deal with the same object in Storage.

I hope all of that makes sense.

Good Grief!!! I thought I had rid myself of ObservableObjects!!! Thank you very much for your detailed reply, Chris. But I have to admit I am so, so badly disappointed. With my discovery of AppStorage I thought I would be able to get rid of ObservableObjects and EnvironmentObjects once and for all!!! Hallelujah!!! But no such luck. I can never get those darn things to behave in any predictable manner. Perhaps a consederation of the behavior of AppStorage will give me sufficient insight to get these global variables to behave like I want them to:)