Application Setting and User Defaults

Hello,

In my application I am going to create a settings screen that will allow the user to change a couple of aspects in terms of how the core feature of the app works.

I have been wondering what might be the best way to save the user settings. I have considered global variables, binding variables and user defaults.

When it comes to global variables I don't think I have ever used them, with the exception of debugging. I kinda consider it a bad practice and I don't think all the screens of my app should have the ability to see and change this values.

Then, there is the possibility of using binding variables. I think this may be like trying to kill a fly with a war tank. I think it is too much and adds a lot of complexity to something that should be pretty straight-forward.

Finally, user defaults, to me, seems the best way to go. I can change it and keep persistency and it can be accessed easily.

What do you think is the best way to go?

Thank you for your help.
Answered by Claude31 in 636309022
User Defaults is precisely done for this. So IMO that's the best way to go.
It allows also user to change values from iOS settings,; without opening the app.

You can copy the most used in global var (computed var so that's it is always synced with settings).
Accepted Answer
User Defaults is precisely done for this. So IMO that's the best way to go.
It allows also user to change values from iOS settings,; without opening the app.

You can copy the most used in global var (computed var so that's it is always synced with settings).
Thank you for your reply!

Where should I initialise User Default values when the app is opened for the very first time? In the App Delegate file?
I register in AppDidFinishLaunching.

And I load userDefaults in any view where I need to use, in viewDidLoad.

This tutorial may be helpful: https ://makeapppie. com/2016/03/14/using-settings-bundles-with-swift/
Application Setting and User Defaults
 
 
Q