Cocoa Programming for Mac OS X - Aaron Hillegass [68]
NSDictionary and NSMutableDictionary
Before you do anything with user defaults, we need to discuss the classes NSDictionary (Figure 13.2) and NSMutableDictionary. A dictionary is a collection of key-value pairs. The keys are strings, and the values are pointers to objects.
Figure 13.2. An Instance of NSDictionary
A string can be a key only once in a dictionary. When you want to know the value to which a key is bound, you will use the method objectForKey:.
anObject = [myDictionary objectForKey:@"foo"];
If the key is not in the dictionary, this method will return nil.
NSMutableDictionary is a subclass of NSDictionary. An instance of NSDictionary is created with all the keys and values it will ever have. You can query the object, but you cannot change it. NSMutableDictionary, on the other hand, allows you to add and remove keys and values.
NSDictionary
A dictionary is implemented as a hash table, so looking up keys is very fast. Here are a few of the most commonly used methods in the class NSDictionary:
- (NSArray *)allKeys
Returns a new array containing the keys in the dictionary.
- (unsigned)count
Returns the number of key-value pairs in the dictionary.
- (id)objectForKey:(NSString *)aKey
Returns the value associated with aKey or returns nil if no value is associated with aKey.
A for-in loop will enumerate through the keys in a dictionary:
NSDictionary *dict = ...
for (NSString *key in dict) {
NSLog(@"%@ -> %@", key, [dict objectForKey:key]);
}
NSMutableDictionary
Here are some commonly used methods in the class NSMutableDictionary:
+ (id)dictionary
Creates an empty dictionary.
- (void)removeObjectForKey:(NSString *)aKey
Removes aKey and its associated value object from the dictionary.
- (void)setObject:(id)anObject forKey:(NSString *)aKey
Adds an entry to the dictionary, consisting of aKey and its corresponding value object anObject. The value object receives a retain message before being added to the dictionary. If aKey already exists in the receiver, the receiver’s previous value object for that key is sent a release message, and anObject takes its place.
NSUserDefaults
Every application comes with a set of defaults “from the factory.” When a user edits his or her defaults, only the differences between the user’s wishes and the factory defaults are stored in the user’s defaults database. Thus, every time the application starts up, you need to remind it of the factory defaults. This operation is called registering defaults.
After registering, you will use the user defaults object to determine how the user wants the app to behave. This process is called reading and using the defaults. The data from the user’s defaults database will be read automatically from the filesystem.
In your Preferences panel, you will allow the user to set the defaults. The changes to the defaults object will be written automatically to the filesystem. This process is known as setting the defaults (Figure 13.3).
Figure 13.3. NSUserDefaults and the Filesystem
Here are some commonly used methods that are implemented in NSUserDefaults:
+ (NSUserDefaults *)standardUserDefaults
Returns the shared defaults object.
- (void)registerDefaults:(NSDictionary *)dictionary
Registers the factory defaults for the application.
- (void)setBool:(BOOL)value forKey:(NSString *)defaultName
- (void)setFloat:(float)value forKey:(NSString *)defaultName
- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName
- (void)setObject:(id)value forKey:(NSString *)defaultName
Methods for changing and saving a user’s wishes.
- (BOOL)boolForKey:(NSString *)defaultName
- (float)floatForKey:(NSString *)defaultName
- (NSInteger)integerForKey:(NSString *)defaultName
- (id)objectForKey:(NSString *)defaultName