We don’t use NSUserDefaults in the app. Instead we have an NSMutableDictionary 
that holds the settings, and we write them to a file when they change. We read 
them in at app startup.
That allows us to actually have different settings for various items (these are 
USB devices like mice and keyboards), and allows us to work around the 
restrictions of NSUserDefaults.

My only real issue with NSUserDefaults is that you cannot hand edit the prefs 
file since the system will overwrite your changes with its own cached data. 
There was also a time in High Sierra
where changes in NSUserDefaults would trigger two KVO calls instead of just 
one. It caused interesting issues with analytics, making it look like the user 
triggered setting changes twice instead of just once.
Thankfully, Apple fixed that in Mojave.

Having said this, unless there are extenuating circumstances (like a 
requirement for multiple prefs files), I would rather use NSUserDefaults 
because of the facilities it gives us. Having a single file with a dictionary 
for each setting entity is a very valid way of doing things. It still means you 
have to write your own settings handler though, to maintain the individual 
settings dictionaries. It seems like the two approaches end up with the same 
amount of work either way.

Jack

> On Apr 4, 2021, at 8:01 AM, Richard Charles <rcharles...@gmail.com> wrote:
> 
> 
>> On Apr 4, 2021, at 4:50 AM, Mike Abdullah <mabdul...@karelia.com> wrote:
>> 
>>> From the docs - init returns an initialized NSUserDefaults object whose 
>>> argument and registration domains are already set up. This method does not 
>>> put anything in the search list. Invoke it only if you’ve allocated your 
>>> own NSUserDefaults instance instead of using the shared one.
>>> 
>>> So it appears that using alloc int does not return the shared instance.
>> 
>> Where in the docs do you read that? The current NSUserDefaults docs say 
>> contrary:
> 
> 
> Sorry for the confusion. It is older 10.9 documentation for the init method. 
> Apple depreciated initWithUser: and added initWithSuiteName: around this same 
> timeframe.
> 
> 
>> On Apr 4, 2021, at 12:15 AM, Jack Brindle via Cocoa-dev 
>> <cocoa-dev@lists.apple.com> wrote:
>> 
>> Gabriel;
>> 
>> It appears you are trying to get NSUserDefaults to do something that Apple 
>> doesn’t want it to do these days. Why not create your own defaults,
>> writing the data to a dictionary that is then written to a file that you 
>> save in the ~/Library/Preferences folder, with a name of your choice?
>> 
>> This does work in Big Sur, we use it ourselves. The down side is that you 
>> don’t have the nice (and infuriating) caching of defaults that the system 
>> provides.
>> Note that the system will still create the standard user defaults file for 
>> you, to save things like window positioning. But, you can save these things
>> yourself in your own files. Just make sure you don’t use the same file name 
>> as the system uses, or it will overwrite yours. You can modify it as you 
>> suggested previously, though.
> 
> 
> So Jack I am curious, do you use alloc then init or initWithSuiteName:nil or 
> initWithSuiteName:@“mySuiteName" to create your own defaults?
> 
> --Richard Charles

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to