UserDefaults.register(defaults:) Is a Bit Like Nil-Coalescing
I suggested the use of UserDefaults.register(defaults:)
today. During the conversation, I realized that this behaves very much like the Optional
/Nil-Coalescing operator ??
.
I suggested the use of UserDefaults.register(defaults:)
today. During the conversation, I realized that this behaves very much like the Optional
/Nil-Coalescing operator ??
.
I really want to like Swift property wrappers for UserDefaults
. But I have a hard time making them 100% useful. Take the post by Andy Ibanez on the topic for example. It’s a well-written post with great code examples. If you want copy-able code for your app, read it. It’s good.
The default Interface Builder approach of using a “Shared User Defaults Controller” object breaks down if your app preferences are not stored in the standard place. Both my apps The Archive and the Word Counter for Mac are comprised of multiple executable applications. For The Archive, it’s the main app and the Quick Entry popup window. They share some settings, like which theme and font size is used. To share these settings, I rely on NSUserDefault
(or just UserDefaults
in Swift now). I cannot use the UserDefaults.standard
, though, because that is tied to the currently running app’s bundle ID. In the case of the main app, it’s the ID of the main app; but for the Quick Entry helper – or any helper app –, it’s the helper’s bundle ID. This way, the defaults dictionaries are not shared.