How to delete UserDefaults data on macOS and Catalyst
source link: https://sarunw.com/posts/how-to-delete-userdefaults-data-on-macos-catalyst/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to delete UserDefaults data on macOS and Catalyst
Table of Contents
UserDefaults is a commonplace to save app preferences and settings. And you might need to delete or reset this many times during development.
In iOS, if you want to delete the app's user defaults, you can simply delete the app. But that is not the case for macOS and Catalyst app.
To delete user defaults on macOS/Catalyst, we need help from the default
command.
Delete UserDefaults with default
command means to use during development phrase where you want to test quickly without a need to use removeObject(forKey:)
.
What is a defaults
command
macOS applications and other programs use the defaults system to record user preferences and other information to be maintained when the application isn't running.
When you use UserDefaults
in iOS, it is saved in the defaults system when translating to Catalyst.
The defaults
command is a way to interact with these values.
What is a domain
Every user defaults belong to a domain. The defaults
delete command need to know a domain to identify the one you want to delete. To domain that your user defaults belong to depends on how you initialize them.
Domain for UserDefaults.standard
When you use standard user defaults, the system uses your app bundle id as a domain.
UserDefaults.standard.set("A", forKey: "a")
Domain for custom UserDefaults
UserDefaults.standard
is not the only way to initialize a user defaults. You can initialize user defaults with a custom domain using init?(suiteName suitename: String?)
.
UserDefaults(suiteName: "Custom")
The suitname
will be used as a domain. In this case, the domain is "Custom".
Commands
After you know how to find the domain, here are two ways you can delete your user defaults.
Open up the Terminal app and put the following commands
Removes all information
To delete every key under user defaults, you use defaults delete domain
.
defaults delete your.app.bundle.id
// or
defaults delete suit.name
Examples
UserDefaults.standard.set("A", forKey: "a")
// defaults delete com.sarunw.app
UserDefaults(suiteName: "Custom").set("A", forKey: "a")
// defaults delete Custom
Remove a specific key
If you want to delete just one key, you can use defaults delete domain key
.
defaults delete your.app.bundle.id key
// or
defaults delete suit.name key
Examples
UserDefaults.standard.set("A", forKey: "a")
// defaults delete com.sarunw.app a
You may also like
NSUserDefaults is a go-to database for saving users' preferences. Learn how to populate it with your default values.
UserDefaultsLearn how to lets users save an image outside of your app’s sandbox.
CatalystRead more article about UserDefaults, Catalyst, Mac,
or see all available topic
Get new posts weekly
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
How to add a unit test target to a Tuist project
We will see how hard (or easy) it is to add a new unit testing bundle target to your Xcode project with Tuist.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK