Database Reference
In-Depth Information
This same design can be used in a nondocument application by changing the
DocumentPreferences
object by directly adding the methods
-valueForUndefinedKey:
and
-setValue:forUndefinedKey:
to the
NSApplicationDelegate
along with the
NSManagedOb-
jectContext
.
Whether we are working in a document model or not, we can now access
persistent store-specific parameters with a single call similar to the following:
NSString *value = [[self preferences] valueForKey:@
"exampleKey1"
];
We can also set them with a call similar to the following:
[[self preferences] setValue:@
"someValue"
forKey:@
"someKey"
];
In both of these examples, we are calling
-valueForKey:
and
-setValue:forKey:
directly on the
DocumentPreferences
object and not worrying about whether the
value exists. If it does not exist, we will receive a
nil
. If it has been set as a
default, we get the default back, and if we have overridden the default or
previously set the property, it is returned.
Lastly, like the
NSUserDefaults
, the default values are not persisted to disk.
Therefore, we need to set them every time we initialize the
DocumentPreferences
.
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
[defaults setValue:[NSNumber numberWithBool:YES] forKey:@
"default1"
];
[defaults setValue:@
"DefaultValue2"
forKey:@
"default2"
];
[defaults setValue:@
"DefaultValue3"
forKey:@
"default3"
];
[[self preferences] setDefaults:defaults];
However, we do not need to worry about changing the defaults at a later date.
If we change the defaults in a subsequent version, they will automatically be
updated if the user has not overridden them.
Now that we have addressed making our code cleaner and more integrated
on the inside, we need to look at making our application cleaner and more
integrated with the overall operating system. As you will see in
Chapter 9,
Spotlight, Quick Look, and Core Data
, on page 153
, with a small amount of
work we can make our application a true first-class Mac OS X citizen.