Contribute some dictionary keys with registerDefaults

2014-06-18 Thread Jonathan Taylor
I currently have a plist which contains some configuration values; at start of 
day I use the following call to make certain factory settings available to 
the application through NSUserDefaults:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary 
dictionaryWithContentsOfFile:configPath]];

This works fine with single-object keys. i.e. I can include a key of type 
Number in the plist file, and it will be made available in the same way as 
other user defaults.

However some of my user defaults are organised within a dictionary (e.g. there 
is a dictionary for each separate camera connected to the system, tracking 
user-controllable defaults such as chosen framerate). I would like a way to 
contribute factory settings from my plist for some of those key values. However 
if I include a dictionary in my plist, and separately call 
[[NSUserDefaults standardUserDefaults] setObject:aDictionary 
forDefault:@my_camera_key];
then this dictionary object fully overrides the entire dictionary object that I 
supplied for my_camera_key in the plist. I had thought there was an outside 
chance that it would effectively merge the two dictionaries together, looking 
for a specific key first in the dictionary object in the user-specified 
defaults and then falling back to the dictionary object I supplied through 
registerDefaults. This does not appear to be the case though.

Not sure if this makes sense - perhaps an example will help.

plist supplied to registerDefaults contains:
my_camera_key dictionary
my_camera_key.key1 = 4
my_camera_key.key2 = 5

dictionary passed to setObject:forDefault:@my_camera_key contains:
key1=7
key3=1

I had vaguely hoped that if I then called 
[[NSUserDefaults standardUserDefaults] objectForKey:@my_camera_key]
then I might get back a dictionary containing three objects:
key1=7
key2=5
key3=1


Can anyone suggest how I might achieve what I am trying to do here? (Or suggest 
a better mechanism of doing what I want to do, i.e. maintain some sort of 
hierarchy to my defaults, but also be able to supply factory settings for 
objects below the top level of the hierarchy?

Thanks for any suggestions
Jonny
___

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: Contribute some dictionary keys with registerDefaults

2014-06-18 Thread Steve Christensen
When you call -setObject:forKey:, it replaces the entire contents of that key, 
whether the object is a number, string, dictionary, etc. That’s the same 
behavior as if you called -setObject:forKey: on a regular dictionary when one 
of its objects is, itself, a dictionary.

Depending on what you need to be doing, you could go a couple of routes:

1. Add a category on NSUserDefaults to handle getting/setting those values that 
are organized in a sub-dictionary.

2. Create a custom class that manages all of your settings and don’t call 
NSUserDefaults directly outside of that class.

I’ve tended to go with the latter method myself. It allows me to type the 
property value, i.e., int or long instead of NSNumber*, or NSString* instead of 
id; it allows me to range-check and otherwise validate values before storing 
them; and it means that NSUserDefaults property keys are only managed in one 
place so I don’t have to worry about key typos in other files that would have 
me accessing the wrong value or an unknown value.


On Jun 18, 2014, at 5:22 AM, Jonathan Taylor jonathan.tay...@glasgow.ac.uk 
wrote:

 I currently have a plist which contains some configuration values; at start 
 of day I use the following call to make certain factory settings available 
 to the application through NSUserDefaults:
   [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary 
 dictionaryWithContentsOfFile:configPath]];
 
 This works fine with single-object keys. i.e. I can include a key of type 
 Number in the plist file, and it will be made available in the same way as 
 other user defaults.
 
 However some of my user defaults are organised within a dictionary (e.g. 
 there is a dictionary for each separate camera connected to the system, 
 tracking user-controllable defaults such as chosen framerate). I would like a 
 way to contribute factory settings from my plist for some of those key 
 values. However if I include a dictionary in my plist, and separately call 
   [[NSUserDefaults standardUserDefaults] setObject:aDictionary 
 forDefault:@my_camera_key];
 then this dictionary object fully overrides the entire dictionary object that 
 I supplied for my_camera_key in the plist. I had thought there was an outside 
 chance that it would effectively merge the two dictionaries together, looking 
 for a specific key first in the dictionary object in the user-specified 
 defaults and then falling back to the dictionary object I supplied through 
 registerDefaults. This does not appear to be the case though.
 
 Not sure if this makes sense - perhaps an example will help.
 
 plist supplied to registerDefaults contains:
 my_camera_key dictionary
 my_camera_key.key1 = 4
 my_camera_key.key2 = 5
 
 dictionary passed to setObject:forDefault:@my_camera_key contains:
 key1=7
 key3=1
 
 I had vaguely hoped that if I then called 
 [[NSUserDefaults standardUserDefaults] objectForKey:@my_camera_key]
 then I might get back a dictionary containing three objects:
 key1=7
 key2=5
 key3=1
 
 
 Can anyone suggest how I might achieve what I am trying to do here? (Or 
 suggest a better mechanism of doing what I want to do, i.e. maintain some 
 sort of hierarchy to my defaults, but also be able to supply factory settings 
 for objects below the top level of the hierarchy?
 
 Thanks for any suggestions
 Jonny


___

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