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 approach in my own code. You can extend 
NSUserDefaults to add methods that provide a fallback value without first 
registering the values separately via a plist file.

~Martin Wierschin

___

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


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 object, 
which means that the collection itself will be mutable when it is read (i.e. 
you can replace it), but the contents (nodes and leaves) are not. You must do a 
mutable deep copy into a local mutable collection before you are able to modify 
settings in the collection. At this point you are now managing the settings 
yourself. Of course, saving the data in a plist file has the same problem, 
except that you can read in a plist into a collection and have it be mutable 
without too much fuss. As soon as you start using collections in your settings 
you negate much of the benefits provided by NSUserDefaults, especially if the 
collections are very deep.

I have saved preferences using both methods - I prefer using the NSUserDefaults 
method, but I still keep a settings manager for the collections that are saved 
in the defaults. The system gives you the standard user defaults, and 
automatically keeps things like window frames in that version. It also provides 
a nice KVC/KVO facility that is a bit more difficult (but doable) with your own 
plist file. In short, just about everything that NSUserDefaults gives you, you 
can create yourself by saving your own settings plist file. For simple apps, it 
is a no-brainer to use NSUserDefaults. For a much more complex app, with 
multiple collections needed for settings, it is a much more difficult call - 
since you are already having to manage the settings yourself, it may actually 
becomes easier to save and read the settings file yourself.

The collection issue also plays a part in using defaults write to modify 
settings. To use it, the prefs file needs to be copied, collection extracted 
and placed into its own file, change made, then you can use defaults write to 
save it back. I would not put my QA people through that process.

As for populating the defaults, you still have to do that yourself with 
NSUserDefaults. The [NSUserDefaults registerDefaults:] method does that, If you 
are using collections, you still have to create the collection and save it in 
the NSDictionary you create to hand to registerDefaults. In my experience with 
large apps with complex settings, I already have created the managers for 
handling the settings, so this actually becomes less of a feature - the 
managers handle reading in the data and giving the user whatever settings they 
need. Adding a setting is very easy here.

As usual, in the Mac, there are multiple ways of doing things. Each has its own 
tradeoffs. The designer needs to consider the needs of that application in 
order to make the right choice for that app. If the app needs to save multiple 
collections of data, then it very well may be easier to save those collections 
in multiple files, something that NSUserDefaults in not designed to do.

By the way, I also know developers who use an SQLite database for their 
settings. It was the right way for their app...

Jack 

> On Apr 6, 2021, at 6:41 AM, Gabriel Zachmann via Cocoa-dev 
>  wrote:
> 
> 
>> 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 is, however, a nice feature of NSUserDefaults that you will, I think, 
> loose,
> or that you will have to implement yourself by hand:
> when the app runs for the first time, NSUserDefaults gets populated by the 
> (real) defaults that come (more or less) hard-coded with your app.
> When a user changes one of those settings himself, then the system will know 
> that this is an actual user setting.
> Now, if you later change the values of the (real) defaults that come with the 
> app, maybe because the new default values are better,
> then those new defaults will take effect *except* for those settings that the 
> user has changed himself.
> 
>> (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.
> 
> But you can always change them using the 'defaults' command , can't you?
> ('defaults write' even takes a plist as argument.)
> 
> 
> Best regards, Gabriel
> 
> 
> ___
> 
> 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:
> 

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 is, however, a nice feature of NSUserDefaults that you will, I think, 
loose,
or that you will have to implement yourself by hand:
when the app runs for the first time, NSUserDefaults gets populated by the 
(real) defaults that come (more or less) hard-coded with your app.
When a user changes one of those settings himself, then the system will know 
that this is an actual user setting.
Now, if you later change the values of the (real) defaults that come with the 
app, maybe because the new default values are better,
then those new defaults will take effect *except* for those settings that the 
user has changed himself.

> (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.

But you can always change them using the 'defaults' command , can't you?
('defaults write' even takes a plist as argument.)


Best regards, Gabriel




smime.p7s
Description: S/MIME cryptographic signature
___

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


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 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  wrote:
> 
> 
>> 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 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 
>>  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


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 
>> 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?
> 
> I can see that this would work well.
> 
> It didn't seem to me that one solution is superior to another.
> So, I have now implemented one dictionary per display that my app sees,
> and store those dictionaries in the NSUserDefaults, with the display name as 
> the key.
> 
> Thanks a lot for all your help and ideas!
> 
> Best regards, Gabriel
> 
> 
> ___
> 
> 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/zav%40mac.com
> 
> This email sent to z...@mac.com

___

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


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 choice?

I can see that this would work well.

It didn't seem to me that one solution is superior to another.
So, I have now implemented one dictionary per display that my app sees,
and store those dictionaries in the NSUserDefaults, with the display name as 
the key.

Thanks a lot for all your help and ideas!

Best regards, Gabriel




smime.p7s
Description: S/MIME cryptographic signature
___

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


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

___

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


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 benefits? I have only 
ever been infuriated with this process during development.

--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


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 
>> 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 
>  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


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 setBool:YES forKey:@"MyKey”];
>>> 
>>>   BOOL value = [monitor1 boolForKey:@"MyKey"];
>> 
>> So, where is the kind of monitor encoded?
>> I mean, how does the code distinguish between the preferences of monitor1 
>> and monitor2?
>> 
>> 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.
>> 
> 
> 
> 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:

> Creates a user defaults object initialized with the defaults for the app and 
> current user.
https://developer.apple.com/documentation/foundation/nsuserdefaults/1414356-init?language=objc
 


Mike.
___

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


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?

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.

Jack

> On Apr 3, 2021, at 11:26 AM, Richard Charles via Cocoa-dev 
>  wrote:
> 
> 
>> 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 , Alloc init vs standard Userdefault
> 
> https://stackoverflow.com/questions/36615260
> 
> --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/jackbrindle%40me.com
> 
> This email sent to jackbrin...@me.com

___

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


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 , Alloc init vs standard Userdefault

https://stackoverflow.com/questions/36615260

--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


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 doc says:
> 
> """
> The suiteName domain is similar to a bundle identifier string, but isn't 
> necessarily tied to a particular application or bundle. A suite can be used 
> to hold preferences that are shared between multiple applications
> """
> 
> So, I think, it is meant for app groups.

App groups, I believe, were introduced in 10.7.   The -addSuiteNamed: API has 
been around since 10.0.  
> 
> What I don't get is this sentence in the doc:
> 
> "The additional search lists of the suiteName domain are searched after the 
> current domain..."
I believe that just means that your app’s Domain is searched first, and any 
common keys would mask out keys in your suite.  
> 
> And I'd rather understand exactly what my code is doing than guessing what it 
> might do.

Doesn’t seem mysterious… and would allow you to maintain a suite for each 
monitor type which could be loaded and unloaded as you detect the presence of a 
monitor, or based on which monitor your app is running on.  As long as your 
suite keys had a prefix they’d never collide with keys in you app’s main 
defaults domain.

Best Regards, Sandor

> 
> 
> 
> Best regards, Gabriel
> 
___

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


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: @"Pref1"];
> 
> Will that make NSUserDefaults automagically write the new 
> preferences/defaults to disk?
> Will it notice that one of the dictionaries has changed?

No, get the dict, mutate it, and set it back into prefs. That’s why I suggested 
writing extensions on NSUserDefaults, which would do all this for you.

Steve via iPad

___

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


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 
to disk?
Will it notice that one of the dictionaries has changed?

Best regards, Gabriel




smime.p7s
Description: S/MIME cryptographic signature
___

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


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 elegant solution.
> 
> With the dictionary-per-monitor your suggesting, 
> I guess I would have to do something like this:
> 
>NSDictionary *monitor_dict = [NSDictionary dictionaryWithObjectsAndKeys: 
> displayname, defaultsForMonitor, nil ];   // where defaultsForMonitor is an 
> ancillary dictionary
>[defaults registerDefaults: monitor_dict];
>NSDictionary * monitor_user_defaults = [defaults dictionaryForKey: 
> displayname];
>NSNumber * pref1num = [monitor_user_defaults valueForKey: @"Pref1"];
>real_pref1_ = [pref1num intValue];
> 
> I.e., I don't get the nice NSUserDefaults machinery.
> 
> Plus, it's all or nothing, I can't have some preferences defined by the user 
> and some from the defaults

I’d just add extensions to NSUserDefaults so they do the messy part and you can 
make smoother calls:

-(NSInteger) integerForKey:(NSString*)key forScreen:(NSInteger)screen
{
NSDictionary*   dict = [self objectForKey:[NSString 
stringWithFormat:@“screen %ld”, screen]];

return [dict[key] integerValue];
}

And of course the opposite for setting values per screen. I’d type more, but it 
takes forever to type code on an iPad.

Steve via iPad

___

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


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 to do something like this:

NSDictionary *monitor_dict = [NSDictionary dictionaryWithObjectsAndKeys: 
displayname, defaultsForMonitor, nil ];   // where defaultsForMonitor is an 
ancillary dictionary
[defaults registerDefaults: monitor_dict];
NSDictionary * monitor_user_defaults = [defaults dictionaryForKey: 
displayname];
NSNumber * pref1num = [monitor_user_defaults valueForKey: @"Pref1"];
real_pref1_ = [pref1num intValue];

I.e., I don't get the nice NSUserDefaults machinery.

Plus, it's all or nothing, I can't have some preferences defined by the user 
and some from the defaults.

Best regards, Gabriel



smime.p7s
Description: S/MIME cryptographic signature
___

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


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 shared instance is the one that is persistent, right?

> 
> It would be up to you to determine the monitors attached to the computer 
> using NSScreen and or CGDirectDisplay routines.

Right, that is what I'm doing.

Best regards, Gabriel



smime.p7s
Description: S/MIME cryptographic signature
___

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


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"];
> 
> So, where is the kind of monitor encoded?
> I mean, how does the code distinguish between the preferences of monitor1 and 
> monitor2?
> 
> 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.
> 


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.

It would be up to you to determine the monitors attached to the computer using 
NSScreen and or CGDirectDisplay routines.

--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


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 
collections, a combination of instances of) NSData 
, NSString 
, NSNumber 
, NSDate 
, NSArray 
, or NSDictionary 
.

Am I missing something here?

Alex Zavatone

> On Apr 2, 2021, at 4:05 PM, Gabriel Zachmann via Cocoa-dev 
>  wrote:
> 
> 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 unusual approach,
> but that does not seem to work any more.
> 
> So, I am wondering, what is the correct and easy approach to managing 
> different user settings (user defaults)?
> 
> Note that my app is sandboxed, so the user defaults must go in 
>  ~/Library/Containers/de.zach.MyApp/Data/Library/Preferences/
> , must they not?
> 
> I looked at -initWithSuiteName , but it seems to me that is not they proper 
> approach, is it?
> 
> Thanks a lot in advance for all kinds of insights and help!
> 
> Best regards, Gabriel
> 
> 
> 
> ___
> 
> 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/zav%40mac.com
> 
> This email sent to z...@mac.com

___

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


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 code distinguish between the preferences of monitor1 and 
monitor2?

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.


Best regards, Gabriel



smime.p7s
Description: S/MIME cryptographic signature
___

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


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 
necessarily tied to a particular application or bundle. A suite can be used to 
hold preferences that are shared between multiple applications
"""

So, I think, it is meant for app groups.

What I don't get is this sentence in the doc:

"The additional search lists of the suiteName domain are searched after the 
current domain..."

And I'd rather understand exactly what my code is doing than guessing what it 
might do.



Best regards, Gabriel



smime.p7s
Description: S/MIME cryptographic signature
___

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


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"] boolForKey:@"some 
> key"]


How about something like this?

 NSUserDefaults *monitor1 = [[NSUserDefaults alloc] init];
 [monitor1 setBool:YES forKey:@"MyKey”];

 BOOL value = [monitor1 boolForKey:@"MyKey"];


--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


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.
> 
> Yes, I understand that.
> 
>> That being said, [NSUserDefaults.standardUserDefaults boolForKey:@"some 
>> key"] etc should all work as expected.
> 
> Yes, they do.
> 
> 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"] boolForKey:@"some 
> key"]

I thought -addSuiteNamed: would allow you to maintain/add a suite of defaults 
for each monitor. 

Best regards, Sandor

> Best regards, Gabriel
> 
> PS:
> I used to be able to do something like that with a clutch using 
> ScreenSaverDefaults, but it seems like this has stopped working; I believe it 
> must have been one of the recent Catalina upgrades.
> 
> 
> ___
> 
> 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/admin.szatmari.net%40gmail.com
> 
> This email sent to admin.szatmari@gmail.com
___

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


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 boolForKey:@"some key"] 
> etc should all work as expected.

Yes, they do.

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"] boolForKey:@"some 
key"]

Best regards, Gabriel

PS:
I used to be able to do something like that with a clutch using 
ScreenSaverDefaults, but it seems like this has stopped working; I believe it 
must have been one of the recent Catalina upgrades.




smime.p7s
Description: S/MIME cryptographic signature
___

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


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, Gabriel Zachmann via Cocoa-dev 
>  wrote:
> 
> 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 unusual approach,
> but that does not seem to work any more.
> 
> So, I am wondering, what is the correct and easy approach to managing 
> different user settings (user defaults)?
> 
> Note that my app is sandboxed, so the user defaults must go in 
>  ~/Library/Containers/de.zach.MyApp/Data/Library/Preferences/
> , must they not?
> 
> I looked at -initWithSuiteName , but it seems to me that is not they proper 
> approach, is it?
> 
> Thanks a lot in advance for all kinds of insights and help!
> 
> Best regards, Gabriel
> 
> 
> 
> ___
> 
> 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/admin.szatmari.net%40gmail.com
> 
> This email sent to admin.szatmari@gmail.com
___

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


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 unusual approach,
but that does not seem to work any more.

So, I am wondering, what is the correct and easy approach to managing different 
user settings (user defaults)?

Note that my app is sandboxed, so the user defaults must go in 
  ~/Library/Containers/de.zach.MyApp/Data/Library/Preferences/
, must they not?

I looked at -initWithSuiteName , but it seems to me that is not they proper 
approach, is it?

Thanks a lot in advance for all kinds of insights and help!

Best regards, Gabriel





smime.p7s
Description: S/MIME cryptographic signature
___

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