Re: Several different NSUserDefaults in the same app?

2021-04-06 Thread Martin Wierschin via Cocoa-dev
This topic of default defaults (aka initial, hardcoded, or fallback values) came up recently on this developer blog: https://mjtsai.com/blog/2021/04/02/foil-userdefaults-property-wrapper/ I've always used this same

Re: Several different NSUserDefaults in the same app?

2021-04-06 Thread Jack Brindle via Cocoa-dev
I agree with you, to an extent. NSUserDefaults is very good when used as a single-level repository for settings data. When you want to categorize the settings by using collections, it starts to show the same problems that a regular file has, except slightly worse. Collections are saved as an

Re: Several different NSUserDefaults in the same app?

2021-04-06 Thread Gabriel Zachmann via Cocoa-dev
> 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 Yes, that certainly works. There

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Jack Brindle via Cocoa-dev
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

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Alex Zavatone via Cocoa-dev
And if you want a nice simple HMAC256 encoding class to make the results securer if you want, I’ll happily toss one your way. > On Apr 4, 2021, at 2:22 PM, Gabriel Zachmann via Cocoa-dev > wrote: > > >> >> It appears you are trying to get NSUserDefaults to do something that Apple >>

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Gabriel Zachmann via Cocoa-dev
> > 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

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Steve Mills via Cocoa-dev
> On Apr 4, 2021, at 10:11, Richard Charles via Cocoa-dev > wrote: > > Why does Apple cache the defaults, what are the positive benefits? I have > only ever been infuriated with this process during development. For the same reason as any cache; speed and efficiency. Steve via iPad

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Richard Charles via Cocoa-dev
> On Apr 4, 2021, at 12:15 AM, Jack Brindle via Cocoa-dev > wrote: > > 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. Why does Apple cache the defaults, what are the positive

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Richard Charles via Cocoa-dev
> On Apr 4, 2021, at 4:50 AM, Mike Abdullah 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 >>

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Mike Abdullah via Cocoa-dev
> On 3 Apr 2021, at 16:34, Richard Charles via Cocoa-dev > wrote: > >> >> On Apr 3, 2021, at 8:59 AM, Gabriel Zachmann wrote: >> >> Thanks a lot for your response! >> >>> How about something like this? >>> >>> NSUserDefaults *monitor1 = [[NSUserDefaults alloc] init]; >>> [monitor1

Re: Several different NSUserDefaults in the same app?

2021-04-04 Thread Jack Brindle via Cocoa-dev
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?

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Richard Charles via Cocoa-dev
> On Apr 3, 2021, at 9:56 AM, Gabriel Zachmann wrote: > > Sorry for asking: the shared instance is the one that is persistent, right? Looks like an instance created from alloc init would not be useful in your situation. It appears you would be in charge of persistence. NSUserDefault ,

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Sandor Szatmari via Cocoa-dev
Gabriel > On Apr 3, 2021, at 10:55, Gabriel Zachmann wrote: > >  >> >>> >>> [ [NSUserDefaults userDefaultsWithName:@"monitor-name"] boolForKey:@"some >>> key"] >> >> I thought -addSuiteNamed: would allow you to maintain/add a suite of >> defaults for each monitor. > > Maybe. > > The

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Steve Mills via Cocoa-dev
> On Apr 3, 2021, at 12:00, Gabriel Zachmann via Cocoa-dev > wrote: > > Oh, and BTW: what happens if I do this: > >NSDictionary * monitor_user_defaults = [ [NSUserDefaults standardDefaults] > dictionaryForKey: displayName_]; >[monitor_user_defaults setObject: anArray forKey:

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Gabriel Zachmann via Cocoa-dev
Oh, and BTW: what happens if I do this: NSDictionary * monitor_user_defaults = [ [NSUserDefaults standardDefaults] dictionaryForKey: displayName_]; [monitor_user_defaults setObject: anArray forKey: @"Pref1"]; Will that make NSUserDefaults automagically write the new preferences/defaults

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Steve Mills via Cocoa-dev
> On Apr 3, 2021, at 11:02, Gabriel Zachmann via Cocoa-dev > wrote: > >  >> >> Why not use your top level NSUserDefaults as a dictionary and use each >> monitor name as a key for each object in the dictionary? Is that too clunky? > > I was thinking of that, but I was hoping for a more

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Gabriel Zachmann via Cocoa-dev
> Why not use your top level NSUserDefaults as a dictionary and use each > monitor name as a key for each object in the dictionary? Is that too clunky? I was thinking of that, but I was hoping for a more elegant solution. With the dictionary-per-monitor your suggesting, I guess I would have

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Gabriel Zachmann via Cocoa-dev
>> Can you explain to me, what is the difference between >> [[NSUserDefaults alloc] init] >> and >> [NSUserDefaults standardDefaults] >> ? >> I didn't get that from Apple's docs. >> > > > [...] > So it appears that using alloc int does not return the shared instance. Sorry for asking: the

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Richard Charles via Cocoa-dev
> On Apr 3, 2021, at 8:59 AM, Gabriel Zachmann wrote: > > Thanks a lot for your response! > >> How about something like this? >> >>NSUserDefaults *monitor1 = [[NSUserDefaults alloc] init]; >>[monitor1 setBool:YES forKey:@"MyKey”]; >> >>BOOL value = [monitor1 boolForKey:@"MyKey"];

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Alex Zavatone via Cocoa-dev
Why not use your top level NSUserDefaults as a dictionary and use each monitor name as a key for each object in the dictionary? Is that too clunky? https://developer.apple.com/documentation/foundation/nsuserdefaults A default object must be a property list—that is, an instance of (or for

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Gabriel Zachmann via Cocoa-dev
Thanks a lot for your response! > How about something like this? > > NSUserDefaults *monitor1 = [[NSUserDefaults alloc] init]; > [monitor1 setBool:YES forKey:@"MyKey”]; > > BOOL value = [monitor1 boolForKey:@"MyKey"]; So, where is the kind of monitor encoded? I mean, how does the

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Gabriel Zachmann via Cocoa-dev
>> >> [ [NSUserDefaults userDefaultsWithName:@"monitor-name"] boolForKey:@"some >> key"] > > I thought -addSuiteNamed: would allow you to maintain/add a suite of defaults > for each monitor. Maybe. The doc says: """ The suiteName domain is similar to a bundle identifier string, but isn't

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Richard Charles via Cocoa-dev
> On Apr 3, 2021, at 5:40 AM, Gabriel Zachmann via Cocoa-dev > wrote: > > But what I would like to have is a mechanism that lets me manage different > user defaults when the app is running on different monitors. > Something like > > [ [NSUserDefaults userDefaultsWithName:@"monitor-name"]

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Sandor Szatmari via Cocoa-dev
Gabriel, > On Apr 3, 2021, at 07:41, Gabriel Zachmann via Cocoa-dev > wrote: > > Thanks a lot for your response! > >> User Defaults are now stored in an opaque in memory DB, and while the >> various files may still play a role, you can't make any assumptions about >> them anymore. > >

Re: Several different NSUserDefaults in the same app?

2021-04-03 Thread Gabriel Zachmann via Cocoa-dev
Thanks a lot for your response! > User Defaults are now stored in an opaque in memory DB, and while the various > files may still play a role, you can't make any assumptions about them > anymore. Yes, I understand that. > > That being said, [NSUserDefaults.standardUserDefaults

Re: Several different NSUserDefaults in the same app?

2021-04-02 Thread Sandor Szatmari via Cocoa-dev
Would suites work for you? From the NSUserDefaults docs… Maintaining Suites - addSuiteNamed: Inserts the specified domain name into the receiver’s search list. - removeSuiteNamed: Removes the specified domain name from the receiver’s search list. Sandor Szatmari > On Apr 2, 2021, at 17:05,

Several different NSUserDefaults in the same app?

2021-04-02 Thread Gabriel Zachmann via Cocoa-dev
I used to manage different NSUserDefaults in my app depending on which monitor it runs. So, I create filenames of the user defaults like this: NSString * defaults_name = [ NSString stringWithFormat:@"de.zach.MyApp.%@", displayname ]; Then I used to create the NSUserDefaults object with an