Database Reference
In-Depth Information
An important point about the defaults is that they are not persisted to disk.
If they get persisted, then later versions that change the default would require
additional code to check for persisted defaults and reset them. If, however,
we do not persist them, users of newer versions of the application automati-
cally get the newer defaults for free and, more importantly, do not get their
preferences destroyed if they have changed the value from its default.
The DocumentPreferences object accomplishes all of these goals.
CDPreferences/DocumentPreferences.h
@interface DocumentPreferences : NSObject
{
NSDictionary *_defaults;
NSPersistentDocument *_associatedDocument;
}
@property (assign) NSPersistentDocument *associatedDocument;
@property (assign) NSDictionary *defaults;
- (id)initWithDocument:(NSPersistentDocument*)associatedDocument;
- (NSArray*)allParameterNames;
- (NSDictionary*)allParameters;
@end
Our DocumentPreferences object expects to receive a reference to its NSPersistentDocument
upon initialization. From the passed-in reference, our DocumentPreferences will be
able to access the underlying NSManagedObjectContext . We could also just incorporate
this design directly into a subclass of NSPersistentDocument ; however, that can cause
the document object to become quite large and difficult to maintain. Therefore,
even though there is a one-to-one relationship between NSPersistentDocument objects
and DocumentPreferences objects, we keep them separate to reduce code complexity.
The one thing that's missing from this header file is any way to access the
parameters themselves. There are no methods for this access because we are
going to take advantage of KVC. Whenever another piece of code requests a
parameter from our DocumentPreferences object, the -valueForUndefinedKey: method
is called, and that is where we handle access to the parameters table.
-valueForUndefinedKey:
CDPreferences/DocumentPreferences.m
- (id)valueForUndefinedKey:(NSString*)key
{
id parameter = [self findParameter:key];
if (!parameter && [[self defaults] objectForKey:key]) {
return [[self defaults] objectForKey:key];
}
return [parameter valueForKey:@ "value" ];
}
 
 
 
Search WWH ::




Custom Search