

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
-
75
readme.md Defaults
-
68
README.md UserDefaultsStore
-
46
README.md Zephyr ?️ Effortlessly sync UserDefaults over iCloud
-
42
One of my students asked in Slack the other day about how to use UserDefaults properly in a Clean Swift architecture. I’ve seen multiple takes on this question in other blogs. Here’s my take: ...
-
72
我们经常会使用Keychain和UserDefaults来存储iOS App数据。Keychain是iOS设备中的一个安全的存储容器,可以用来为应用保存敏感信息比如用户名,密码,网络密码,认证令牌。
-
55
With the UserDefaults class, you can save settings and properties related to application or user data. Any data saved to the defaults system will persist through application restarts. In this tutorial we will save the sta...
-
29
Apple recently announced project Catalyst during its WWDC. Catalyst, also known as Marzipan, is designed to allow developers to release iOS apps for macOS, sta...
-
7
When to use UserDefaults, Keychain, or Core Data There are many ways to store data locally in iOS app. UserDefaults, Keychain and Core Data are some of the most popular ways to persist...
-
5
The WWDC 2019 had a major impact on the UI toolkit landscape: while the venerable AppKit APIs remained available, Apple removed the old Carbon APIs and introduced 2 brand new frameworks: Mac Catalyst and SwiftUI. Apple sporadically mentione...
-
11
UserDefaults provide an interface to read and write many data types, including an array. But you can only store an array of Property-List objects, e.g., string, number, boolean, date, array, dictionary, and dat...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK